|
15 | 15 | #include <locale>
|
16 | 16 | #include <codecvt>
|
17 | 17 | #include <sstream>
|
18 |
| - |
| 18 | +#define DR_MP3_IMPLEMENTATION |
| 19 | +#include "dr_libs/dr_mp3.h" |
19 | 20 | #if defined(_MSC_VER)
|
20 | 21 | #pragma warning(disable: 4244 4267) // possible loss of data
|
21 | 22 | #endif
|
@@ -713,9 +714,35 @@ read_wav(const std::string &fname, std::vector<float> &pcmf32, std::vector<std::
|
713 | 714 | return true;
|
714 | 715 | }
|
715 | 716 |
|
716 |
| -bool |
717 |
| -read_mp3(const std::string &fname, std::vector<float> &pcmf32, std::vector<std::vector<float>> &pcmf32s, bool stereo) { |
| 717 | +bool read_mp3(const std::string &fname, std::vector<float> &pcmf32, bool stereo) { |
| 718 | + drmp3 mp3; |
| 719 | + if (!drmp3_init_file(&mp3, fname.c_str(), nullptr)) { |
| 720 | + fprintf(stderr, "error: failed to open '%s' as MP3 file\n", fname.c_str()); |
| 721 | + return false; |
| 722 | + } |
| 723 | + |
| 724 | + if (mp3.sampleRate != COMMON_SAMPLE_RATE) { |
| 725 | + fprintf(stderr, "%s: MP3 file '%s' must be %i kHz\n", __func__, fname.c_str(), COMMON_SAMPLE_RATE / 1000); |
| 726 | + return false; |
| 727 | + } |
| 728 | + |
| 729 | + if (mp3.channels != 1 && mp3.channels != 2) { |
| 730 | + fprintf(stderr, "%s: MP3 file '%s' must be mono or stereo\n", __func__, fname.c_str()); |
| 731 | + return false; |
| 732 | + } |
| 733 | + |
| 734 | + if (stereo && mp3.channels != 2) { |
| 735 | + fprintf(stderr, "%s: MP3 file '%s' must be stereo for this operation\n", __func__, fname.c_str()); |
| 736 | + return false; |
| 737 | + } |
| 738 | + |
| 739 | + drmp3_uint64 frameCount; |
| 740 | + float *pSampleData = drmp3__full_read_and_close_f32(&mp3, nullptr, &frameCount); |
718 | 741 |
|
| 742 | + pcmf32.assign(pSampleData, pSampleData + frameCount * mp3.channels); |
| 743 | + drmp3_free(pSampleData, nullptr); |
| 744 | + |
| 745 | + return true; |
719 | 746 | }
|
720 | 747 |
|
721 | 748 | bool
|
|
0 commit comments