|
1 | 1 | #include "AudioDecoder.h"
|
2 |
| -#include <iostream> |
3 | 2 | #include <fstream>
|
4 |
| -//#include <curl/curl.h> |
| 3 | +#include <iostream> |
| 4 | +// #include <curl/curl.h> |
5 | 5 | #include <sndfile.h>
|
6 |
| -#include <filesystem> |
7 | 6 | #include <chrono>
|
8 |
| -#include "AudioBus.h" |
| 7 | +#include <filesystem> |
9 | 8 | #include "AudioArray.h"
|
| 9 | +#include "AudioBus.h" |
10 | 10 |
|
11 | 11 | #include <android/log.h>
|
12 | 12 | #define LOG_TAG "AudioDecoder"
|
|
15 | 15 |
|
16 | 16 | namespace audioapi {
|
17 | 17 |
|
18 |
| - AudioDecoder::AudioDecoder(int sampleRate) : sampleRate_(sampleRate) {} |
| 18 | +AudioDecoder::AudioDecoder(int sampleRate) : sampleRate_(sampleRate) {} |
19 | 19 |
|
20 |
| - AudioBus* AudioDecoder::decode(const std::string& pathOrURL) { |
21 |
| - return decodeWithFilePath("/Files/runaway_kanye_west.mp3"); |
22 |
| - } |
| 20 | +AudioBus *AudioDecoder::decode(const std::string &pathOrURL) { |
| 21 | + return decodeWithFilePath("/Files/runaway_kanye_west.mp3"); |
| 22 | +} |
23 | 23 |
|
24 |
| - AudioBus *AudioDecoder::decodeWithFilePath(const std::string& path) const { |
25 |
| - SF_INFO sfInfo; |
26 |
| - SNDFILE* sndFile = sf_open(path.c_str(), SFM_READ, &sfInfo); |
27 |
| - if (!sndFile) { |
28 |
| - LOGE("Error opening audio file: %s", sf_strerror(sndFile)); |
29 |
| - return nullptr; |
30 |
| - } |
| 24 | +AudioBus *AudioDecoder::decodeWithFilePath(const std::string &path) const { |
| 25 | + SF_INFO sfInfo; |
| 26 | + SNDFILE *sndFile = sf_open(path.c_str(), SFM_READ, &sfInfo); |
| 27 | + if (!sndFile) { |
| 28 | + LOGE("Error opening audio file: %s", sf_strerror(sndFile)); |
| 29 | + return nullptr; |
| 30 | + } |
31 | 31 |
|
32 |
| - auto* buffer = new float[sfInfo.frames * sfInfo.channels]; |
33 |
| - sf_readf_float(sndFile, buffer, sfInfo.frames); |
34 |
| - sf_close(sndFile); |
| 32 | + auto *buffer = new float[sfInfo.frames * sfInfo.channels]; |
| 33 | + sf_readf_float(sndFile, buffer, sfInfo.frames); |
| 34 | + sf_close(sndFile); |
35 | 35 |
|
36 |
| - if (sfInfo.samplerate != sampleRate_) { |
37 |
| - //todo |
38 |
| - } |
| 36 | + if (sfInfo.samplerate != sampleRate_) { |
| 37 | + // todo |
| 38 | + } |
39 | 39 |
|
40 |
| - auto* audioBus = new AudioBus(sampleRate_, static_cast<int>(sfInfo.frames), sfInfo.channels); |
| 40 | + auto *audioBus = new AudioBus( |
| 41 | + sampleRate_, static_cast<int>(sfInfo.frames), sfInfo.channels); |
41 | 42 |
|
42 |
| - for (int i = 0; i < sfInfo.channels; ++i) { |
43 |
| - float* data = audioBus->getChannel(i)->getData(); |
| 43 | + for (int i = 0; i < sfInfo.channels; ++i) { |
| 44 | + float *data = audioBus->getChannel(i)->getData(); |
44 | 45 |
|
45 |
| - for (int j = 0; j < sfInfo.frames; ++j) { |
46 |
| - data[j] = buffer[j * sfInfo.channels + i]; |
47 |
| - } |
48 |
| - } |
| 46 | + for (int j = 0; j < sfInfo.frames; ++j) { |
| 47 | + data[j] = buffer[j * sfInfo.channels + i]; |
| 48 | + } |
| 49 | + } |
49 | 50 |
|
50 |
| - delete[] buffer; |
| 51 | + delete[] buffer; |
51 | 52 |
|
52 |
| - return audioBus; |
53 |
| - } |
| 53 | + return audioBus; |
| 54 | +} |
54 | 55 | } // namespace audioapi
|
0 commit comments