-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioManager.cpp
More file actions
222 lines (190 loc) · 6.58 KB
/
AudioManager.cpp
File metadata and controls
222 lines (190 loc) · 6.58 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "AudioManager.h"
#include <iostream>
// wav offset
static const int32_t OFFSET = 44;
void printAudioFrameInfo(const AVCodecContext* codecContext, const AVFrame* frame)
{
// See the following to know what data type (unsigned char, short, float, etc) to use to access the audio data:
// http://ffmpeg.org/doxygen/trunk/samplefmt_8h.html#af9a51ca15301871723577c730b5865c5
std::cout << "Audio frame info:\n"
<< " Sample count: " << frame->nb_samples << '\n'
<< " Channel count: " << codecContext->channels << '\n'
<< " Format: " << av_get_sample_fmt_name(codecContext->sample_fmt) << '\n'
<< " Bytes per sample: " << av_get_bytes_per_sample(codecContext->sample_fmt) << '\n'
<< " Is planar? " << av_sample_fmt_is_planar(codecContext->sample_fmt) << '\n';
std::cout << "frame->linesize[0] tells you the size (in bytes) of each plane\n";
if (codecContext->channels > AV_NUM_DATA_POINTERS && av_sample_fmt_is_planar(codecContext->sample_fmt))
{
std::cout << "The audio stream (and its frames) have too many channels to fit in\n"
<< "frame->data. Therefore, to access the audio data, you need to use\n"
<< "frame->extended_data to access the audio data. It's planar, so\n"
<< "each channel is in a different element. That is:\n"
<< " frame->extended_data[0] has the data for channel 1\n"
<< " frame->extended_data[1] has the data for channel 2\n"
<< " etc.\n";
}
else
{
std::cout << "Either the audio data is not planar, or there is enough room in\n"
<< "frame->data to store all the channels, so you can either use\n"
<< "frame->data or frame->extended_data to access the audio data (they\n"
<< "should just point to the same data).\n";
}
std::cout << "If the frame is planar, each channel is in a different element.\n"
<< "That is:\n"
<< " frame->data[0]/frame->extended_data[0] has the data for channel 1\n"
<< " frame->data[1]/frame->extended_data[1] has the data for channel 2\n"
<< " etc.\n";
std::cout << "If the frame is packed (not planar), then all the data is in\n"
<< "frame->data[0]/frame->extended_data[0] (kind of like how some\n"
<< "image formats have RGB pixels packed together, rather than storing\n"
<< " the red, green, and blue channels separately in different arrays.\n";
}
bool operator==(const AudioPCM& l, const AudioPCM& r)
{
return (l.getName() == r.getName());
}
AudioPCM::AudioPCM(const std::string& name) :
mName(name)
{
this->readFilePackets();
}
AudioPCM::AudioPCM(const AudioPCM& other) :
mName(other.getName()),
mSize(other.getSize()),
mBuffer(other.getBuffer())
{
}
AudioPCM& AudioPCM::operator=(AudioPCM other)
{
swap(*this, other);
return *this;
}
AudioPCM::AudioPCM(AudioPCM&& other)
{
swap(*this, other);
}
AudioPCM::~AudioPCM()
{
}
int AudioPCM::readFilePackets()
{
av_register_all();
AVFrame* frame = av_frame_alloc();
if (!frame)
{
std::cout << "Error allocating the frame" << std::endl;
return 1;
}
// you can change the file name "01 Push Me to the Floor.wav" to whatever the file is you're reading, like "myFile.ogg" or
// "someFile.webm" and this should still work
AVFormatContext* formatContext = NULL;
if (avformat_open_input(&formatContext, mName.c_str(), NULL, NULL) != 0)
{
av_free(frame);
std::cout << "Error opening the file" << std::endl;
return 1;
}
if (avformat_find_stream_info(formatContext, NULL) < 0)
{
av_free(frame);
avformat_close_input(&formatContext);
std::cout << "Error finding the stream info" << std::endl;
return 1;
}
// Find the audio stream
AVCodec* cdc = nullptr;
int streamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &cdc, 0);
if (streamIndex < 0)
{
av_free(frame);
avformat_close_input(&formatContext);
std::cout << "Could not find any audio stream in the file" << std::endl;
return 1;
}
AVStream* audioStream = formatContext->streams[streamIndex];
AVCodecContext* codecContext = audioStream->codec;
codecContext->codec = cdc;
if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
{
av_free(frame);
avformat_close_input(&formatContext);
std::cout << "Couldn't open the context with the decoder" << std::endl;
return 1;
}
std::cout << "This stream has " << codecContext->channels << " channels and a sample rate of " << codecContext->sample_rate << "Hz" << std::endl;
std::cout << "The data is in the format " << av_get_sample_fmt_name(codecContext->sample_fmt) << std::endl;
AVPacket readingPacket;
av_init_packet(&readingPacket);
// Read the packets in a loop
while (av_read_frame(formatContext, &readingPacket) == 0)
{
if (readingPacket.stream_index == audioStream->index)
{
AVPacket decodingPacket = readingPacket;
// Audio packets can have multiple audio frames in a single packet
while (decodingPacket.size > 0)
{
// Try to decode the packet into a frame
// Some frames rely on multiple packets, so we have to make sure the frame is finished before
// we can use it
int gotFrame = 0;
int result = avcodec_decode_audio4(codecContext, frame, &gotFrame, &decodingPacket);
if (result >= 0 && gotFrame)
{
decodingPacket.size -= result;
decodingPacket.data += result;
// We now have a fully decoded audio frame
printAudioFrameInfo(codecContext, frame);
}
else
{
decodingPacket.size = 0;
decodingPacket.data = nullptr;
}
}
}
// You *must* call av_free_packet() after each call to av_read_frame() or else you'll leak memory
av_free_packet(&readingPacket);
}
// Some codecs will cause frames to be buffered up in the decoding process. If the CODEC_CAP_DELAY flag
// is set, there can be buffered up frames that need to be flushed, so we'll do that
if (codecContext->codec->capabilities & CODEC_CAP_DELAY)
{
av_init_packet(&readingPacket);
// Decode all the remaining frames in the buffer, until the end is reached
int gotFrame = 0;
while (avcodec_decode_audio4(codecContext, frame, &gotFrame, &readingPacket) >= 0 && gotFrame)
{
// We now have a fully decoded audio frame
printAudioFrameInfo(codecContext, frame);
}
}
// Clean up!
av_free(frame);
avcodec_close(codecContext);
avformat_close_input(&formatContext);
}
void swap(AudioPCM& first, AudioPCM& second)
{
using std::swap;
swap(first.mName, second.mName);
swap(first.mSize, second.mSize);
swap(first.mBuffer, second.mBuffer);
}
void AudioManager::addAudioList(const std::vector<std::string>& filenames)
{
for (auto v : filenames)
{
mAudioSet.insert(AudioPCM(v));
}
}
AudioPCM AudioManager::getAudio(const std::string& filename) const
{
auto it = mAudioSet.find(filename);
if (it == mAudioSet.end())
{
// no such element
}
return *it;
}