-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMusic.cpp
More file actions
540 lines (459 loc) · 15.6 KB
/
Music.cpp
File metadata and controls
540 lines (459 loc) · 15.6 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
BSD 2-Clause License
Copyright (c) 2021, timre13
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Music.h"
#include <iostream>
#include <cassert>
#include <sstream>
Music::Music()
{
reset();
}
void Music::reset()
{
m_state = STATE_UNINITIALIZED;
m_formatContext = nullptr;
m_codecParams = nullptr;
m_codec = nullptr;
m_codecContext = nullptr;
m_outputFormat = nullptr;
m_outputFormatContext = nullptr;
m_outputStream = nullptr;
m_resampleContext = nullptr;
m_currentPacket = nullptr;
m_audioStreamI = 0;
std::cout << "Music reset" << '\n';
}
static std::string getFileInfo(const AVFormatContext *formatContext)
{
if (!formatContext)
return "N/A\n";
std::stringstream output;
if (formatContext->metadata)
{
output << "Metadata:" << '\n';
AVDictionaryEntry *metadataTag{nullptr};
while (metadataTag = av_dict_get(
formatContext->metadata,
"",
metadataTag,
AV_DICT_IGNORE_SUFFIX))
output << " " <<
metadataTag->key << ": " <<
metadataTag->value << '\n';
}
output << "Format: " << formatContext->iformat->long_name << '\n';
output << "Duration: " <<
(long double)formatContext->duration / AV_TIME_BASE << "s" << '\n';
output << "Bit rate: " << formatContext->bit_rate << '\n';
return output.str();
}
static std::string getStreamInfo(
const AVCodecParameters *codecParams,
const AVCodec *codec,
const AVCodecContext *codecContext)
{
if (!codecParams || !codec || !codecContext)
return " N/A\n";
std::stringstream output;
output << " Codec name: " << codec->long_name << '\n';
output << " Bit rate: " << codecParams->bit_rate << '\n';
if (codec->type == AVMEDIA_TYPE_AUDIO)
{
output << " Channels: " << codecParams->channels << '\n';
output << " Sample rate: " << codecParams->sample_rate << '\n';
}
char sampleFormatStr[32];
output << " Sample format: " << av_get_sample_fmt_string(
sampleFormatStr,
sizeof(sampleFormatStr),
codecContext->sample_fmt) << '\n';
return output.str();
}
int Music::openAudioDevice(const std::string &audioDevName)
{
// Get the output format of the audio device
m_outputFormat = av_guess_format(audioDevName.c_str(), nullptr, nullptr);
if (!m_outputFormat)
{
std::cerr << "Failed to get format of output device" << '\n';
return 1;
}
m_outputFormatContext = avformat_alloc_context();
if (!m_outputFormatContext)
{
std::cerr << "Failed to create output format context" << '\n';
return 1;
}
// Tell the format context which output device to use
m_outputFormatContext->oformat = m_outputFormat;
// Create a stream where the output will be written to
m_outputStream = avformat_new_stream(m_outputFormatContext, nullptr);
if (!m_outputStream)
{
std::cerr << "Failed to create output stream" << '\n';
return 1;
}
// Configure the parameters of the output stream
m_outputStream->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
m_outputStream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
m_outputStream->codecpar->format = AV_SAMPLE_FMT_S16;
m_outputStream->codecpar->bit_rate = m_codecContext->bit_rate;
m_outputStream->codecpar->sample_rate = m_codecContext->sample_rate;
m_outputStream->codecpar->channels = m_codecContext->channels;
m_outputStream->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
// Tell the device the parameters
if (avformat_write_header(m_outputFormatContext, nullptr) < 0)
{
std::cerr << "Failed to write stream header to output device" << '\n';
return 1;
}
return 0;
}
int Music::initResampleContext()
{
m_resampleContext =
swr_alloc_set_opts(
nullptr,
m_outputStream->codecpar->channel_layout, // Out channel layout
(AVSampleFormat)m_outputStream->codecpar->format, // Out sample format
m_outputStream->codecpar->sample_rate, // Out sample rate
AV_CH_LAYOUT_STEREO, // In channel layout
(AVSampleFormat)m_codecContext->sample_fmt, // In format
m_codecContext->sample_rate, // In sample rate
0,
nullptr);
if (!m_resampleContext)
{
std::cerr << "Failed to create resample context" << '\n';
return 1;
}
if (swr_init(m_resampleContext))
{
std::cerr << "Failed to init resample context" << '\n';
return 1;
}
return 0;
}
Music::OpenError Music::open(
const std::string &filePath,
const std::string &audioDevName)
{
std::cout << std::string(30, '-') << " begin " << std::string(30, '-') << '\n';
std::cout << "Opening file: " << filePath << '\n';
assert(m_state == STATE_UNINITIALIZED);
m_formatContext = avformat_alloc_context();
if (!m_formatContext)
{
std::cerr << "Failed to allocate format context" << '\n';
m_state = STATE_ERROR;
return OPENERROR_ALLOC;
}
// Open the file as input to the format context
if (avformat_open_input(
&m_formatContext,
filePath.c_str(),
nullptr,
nullptr))
{
std::cerr << "Failed to open file" << '\n';
m_state = STATE_ERROR;
return OPENERROR_FILE;
}
std::cout << ::getFileInfo(m_formatContext);
// Find the info of the streams, so we can use them
if (avformat_find_stream_info(m_formatContext, nullptr))
{
std::cerr << "Failed to find stream info" << '\n';
m_state = STATE_ERROR;
return OPENERROR_OTHER;
}
std::cout << "Number of streams: " << m_formatContext->nb_streams << '\n';
// Loop through the streams
for (unsigned int i{}; i < m_formatContext->nb_streams; ++i)
{
m_codecParams = m_formatContext->streams[i]->codecpar;
// Find a codec for the stream
m_codec = avcodec_find_decoder(m_codecParams->codec_id);
if (!m_codec)
{
std::cerr << "Failed to find decoder for stream, skipping" << '\n';
continue;
}
// Print stream index
std::cout << "Stream #" << i << ":" << '\n';
// If this is not an audio stream
if (m_codec->type != AVMEDIA_TYPE_AUDIO)
{
std::cout << "\tNot an audio stream, skipping" << '\n';
continue;
}
m_codecContext = avcodec_alloc_context3(m_codec);
if (!m_codecContext)
{
std::cerr << "\tFailed to allocate codec context, skipping" << '\n';
continue;
}
if (avcodec_parameters_to_context(m_codecContext, m_codecParams))
{
std::cerr << "\tFailed to fill codex context, skipping" << '\n';
avcodec_free_context(&m_codecContext);
continue;
}
// Print info of the stream
std::cout << getStreamInfo(m_codecParams, m_codec, m_codecContext);
if (avcodec_open2(m_codecContext, m_codec, nullptr))
{
std::cerr << "\tFailed to open codex context, skipping" << '\n';
avcodec_free_context(&m_codecContext);
continue;
}
m_audioStreamI = i;
// TODO: Isn't this ugly?
break;
}
if (!m_codecContext)
{
std::cout << "No audio stream found" << '\n';
avcodec_free_context(&m_codecContext);
m_state = STATE_ERROR;
return OPENERROR_FILE;
}
if (openAudioDevice(audioDevName))
{
m_state = STATE_ERROR;
return OPENERROR_OUTPUT;
}
if (initResampleContext())
{
m_state = STATE_ERROR;
return OPENERROR_OTHER;
}
std::cout << "Successfully opened file and found audio stream" << '\n';
// Opening is done
m_state = STATE_PLAYING;
return OPENERROR_OK;
}
void Music::unPause()
{
if (m_state == STATE_ERROR)
{
std::cerr << "Failed to unpause, object is in error state" << '\n';
return;
}
if (m_state == STATE_UNINITIALIZED)
{
std::cerr << "Failed to unpause, object is uninitialized." << '\n';
return;
}
if (m_state == STATE_END)
{
std::cerr << "Failed to unpause, music ended" << '\n';
return;
}
if (m_formatContext->nb_streams < 1)
{
std::cout << "Failed to unpause, no streams found" << '\n';
m_state = STATE_ERROR;
return;
}
m_state = STATE_PLAYING;
}
void Music::pause()
{
if (m_state == STATE_ERROR)
{
std::cerr << "Failed to pause, object is in error state" << '\n';
return;
}
if (m_state == STATE_UNINITIALIZED)
{
std::cerr << "Failed to pause, object is uninitialized." << '\n';
return;
}
if (m_state == STATE_END)
{
std::cerr << "Failed to pause, music ended" << '\n';
return;
}
m_state = STATE_PAUSED;
}
static AVPacket *resampleFrame(
AVFrame *frame,
AVCodecContext *codecContext,
SwrContext *resampleContext,
AVStream *outputStream)
{
uint8_t *resampledBuffer{};
long outSamples{av_rescale_rnd(
swr_get_delay(
resampleContext,
frame->sample_rate) + frame->nb_samples,
codecContext->sample_rate,
outputStream->codecpar->sample_rate,
AV_ROUND_UP)};
// Allocate buffer for converted data
av_samples_alloc(
&resampledBuffer,
nullptr,
outputStream->codecpar->channels,
outSamples,
(AVSampleFormat)outputStream->codecpar->format,
0);
// Do the conversion from the input format to the output format
swr_convert(
resampleContext, // Resample context
&resampledBuffer, // Output buffer
outSamples, // Number of samples to output
(const uint8_t**)frame->extended_data, // Input buffer
frame->nb_samples); // Number of input samples
// We need the buffer size to create a packet from the buffer
int resampledBufferSize{av_samples_get_buffer_size(
nullptr,
outputStream->codecpar->channels,
outSamples,
(AVSampleFormat)outputStream->codecpar->format,
0)};
if (resampledBufferSize < 0)
{
std::cerr << "Failed to get buffer size" << '\n';
return nullptr;
}
AVPacket *resampledPacket{av_packet_alloc()};
// Create a packet from the buffer, so we can later send it to the device
if (av_packet_from_data(resampledPacket, resampledBuffer, resampledBufferSize))
{
std::cerr << "Failed to create packet from buffer" << '\n';
return nullptr;
}
return resampledPacket;
}
void Music::tick()
{
switch (m_state)
{
case STATE_PLAYING:
// Continue
break;
case STATE_UNINITIALIZED:
// No file open
std::cerr << "Failed to tick, uninitialized" << '\n';
return;
case STATE_ERROR:
// An error occurred, probably no stream found
std::cerr << "Failed to tick, object is in error state" << '\n';
return;
case STATE_PAUSED:
// If paused, don't do anything
// No break
case STATE_END:
// If end of file, don't do anything
// No break
default:
return;
}
AVFrame *frame{av_frame_alloc()};
av_packet_free(&m_currentPacket);
m_currentPacket = av_packet_alloc();
// Read a frame
// FIXME: End of stream detection is buggy
if (av_read_frame(m_formatContext, m_currentPacket) < 0)
{
// End of stream
std::cout << "End of stream" << '\n';
av_write_frame(m_outputFormatContext, nullptr); // Flush the buffer
av_frame_free(&frame);
m_state = STATE_END;
return;
}
// Send the packet to the codec
if (avcodec_send_packet(m_codecContext, m_currentPacket))
{
std::cerr << "Failed to send packet to codec" << '\n';
av_frame_free(&frame);
return; // Maybe next time
}
// Get decoded output data from codecfree
// TODO: Handle error depending on the return code
if (avcodec_receive_frame(m_codecContext, frame))
{
std::cerr << "Failed to receive frame from codec" << '\n';
av_frame_free(&frame);
return; // Maybe next time
}
auto resampledFrame{resampleFrame(
frame,
m_codecContext,
m_resampleContext,
m_outputStream)};
if (resampledFrame)
{
// Write the data to the output device
if (av_write_frame(m_outputFormatContext, resampledFrame) < 0)
{
std::cerr << "Failed to write packet to output device" << '\n';
}
av_packet_free(&resampledFrame);
}
av_frame_free(&frame);
}
std::string Music::getFileInfo() const
{
return ::getFileInfo(m_formatContext);
}
std::string Music::getAudioStreamInfo() const
{
return getStreamInfo(m_codecParams, m_codec, m_codecContext);
}
void Music::seekToS(double timestamp)
{
// FIXME: Maybe this is broken when used with MP3.
// (weird timestamps, see: `getCurrentTimestampS()`)
av_seek_frame(
m_formatContext,
m_audioStreamI,
// VVVVVVV - AV_TIME_BASE
// VV - ???
timestamp * 1000000 / 21, // Timestamp
AVSEEK_FLAG_FRAME);
}
void Music::closeAndReset()
{
if (m_state != STATE_UNINITIALIZED)
{
if (m_outputFormatContext)
av_write_frame(m_outputFormatContext, nullptr); // Flush the buffer
avcodec_free_context(&m_codecContext);
avformat_free_context(m_outputFormatContext);
avformat_close_input(&m_formatContext);
av_packet_free(&m_currentPacket);
swr_free(&m_resampleContext);
std::cout << "File closed" << '\n';
reset();
}
std::cout << std::string(30, '-') << " end " << std::string(30, '-') << '\n';
}
Music::~Music()
{
if (m_state != STATE_UNINITIALIZED)
closeAndReset();
}