-
Notifications
You must be signed in to change notification settings - Fork 554
/
Copy pathavfoundationloader.cpp
136 lines (99 loc) · 3.09 KB
/
avfoundationloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#import "avfoundationloader.hpp"
#import "algorithmfactory.h"
#import <iomanip> // setw()
using namespace std;
namespace essentia {
namespace streaming {
const char* AVAudioLoader::name = "AVAudioLoader";
const char* AVAudioLoader::category = "Input/output";
const char* AVAudioLoader::description = DOC("An AudioLoader using AVFoundation.");
AVAudioLoader::~AVAudioLoader() {
closeAudioFile();
}
void AVAudioLoader::configure() {
_computeMD5 = parameter("computeMD5").toBool();
_selectedStream = parameter("audioStream").toInt();
reset();
}
void AVAudioLoader::openAudioFile(const string& filename) {
E_DEBUG(EAlgorithm, "AVAudioLoader: opening file: " << filename);
_file = new AVFoundationAudioFile(filename, BUFFER_SIZE);
}
void AVAudioLoader::closeAudioFile() {
if (_file) {
delete _file;
_file = NULL;
}
}
void AVAudioLoader::pushChannelsSampleRateInfo(int nChannels, Real sampleRate) {
if (nChannels > 2) {
throw EssentiaException("AVAudioLoader: could not load audio. Audio file has more than 2 channels.");
}
if (sampleRate <= 0) {
throw EssentiaException("AVAudioLoader: could not load audio. Audio sampling rate must be greater than 0.");
}
_nChannels = nChannels;
_channels.push(nChannels);
_sampleRate.push(sampleRate);
}
void AVAudioLoader::pushCodecInfo(std::string codec, int bit_rate) {
_codec.push(codec);
_bit_rate.push(bit_rate);
}
AlgorithmStatus AVAudioLoader::process() {
if (!parameter("filename").isConfigured()) {
throw EssentiaException("AVAudioLoader: Trying to call process() on an AVAudioLoader algo which hasn't been correctly configured.");
}
if (_computeMD5) {
throw EssentiaException("AVAudioLoader: computeMD5 is not implemented.");
}
int framesRead = _file->readNext();
if (!framesRead) {
shouldStop(true);
// copyOutput();
closeAudioFile();
string md5 = "";
_md5.push(md5);
return FINISHED;
}
copyOutput();
return OK;
}
void AVAudioLoader::copyOutput() {
int nsamples = _file->frameLength;
int stride = _file->stride;
bool ok = _audio.acquire(nsamples);
if (!ok) {
throw EssentiaException("AudioLoader: could not acquire output for audio");
}
vector<StereoSample>& audio = *((vector<StereoSample>*)_audio.getTokens());
if (_nChannels == 1) {
float *buffer = _file->buffers[0];
for (int i=0; i<nsamples; i++) {
audio[i].left() = *buffer;
buffer += stride;
}
}
else { // _nChannels == 2
float *left = _file->buffers[0];
float *right = _file->buffers[1];
for (int i=0, loc=0; i<nsamples; i++) {
audio[i].left() = *left;
audio[i].right() = *right;
left += stride;
right += stride;
}
}
_audio.release(nsamples);
}
void AVAudioLoader::reset() {
Algorithm::reset();
if (!parameter("filename").isConfigured()) return;
string filename = parameter("filename").toString();
closeAudioFile();
openAudioFile(filename);
pushChannelsSampleRateInfo(_file->channels, _file->sample_rate);
pushCodecInfo(_file->codec, _file->bit_rate);
}
} // namespace streaming
} // namespace essentia