Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2022 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "vorbisdecoder.h"
24 :
25 : #include <algorithm>
26 :
27 : #if (defined (_MSCVER) || defined (_MSC_VER))
28 : #pragma warning(push)
29 : #pragma warning(disable: 4456) // declaration hides previous local declaration
30 : #endif
31 : #include "thirdparty/stb/stb_vorbis.c"
32 : #if (defined (_MSCVER) || defined (_MSC_VER))
33 : #pragma warning(pop)
34 : #endif
35 :
36 : using namespace muse::audio::codec;
37 :
38 0 : int VorbisDecoder::decode_memory(const unsigned char* mem, unsigned int len, short** output, unsigned int* channels,
39 : unsigned int* sample_rate)
40 : {
41 0 : int channels_ = 0;
42 0 : int sample_rate_ = 0;
43 0 : int samples = stb_vorbis_decode_memory(mem, len, &channels_, &sample_rate_, output);
44 0 : if (channels) {
45 0 : *channels = channels_;
46 : }
47 :
48 0 : if (sample_rate) {
49 0 : *sample_rate = sample_rate_;
50 : }
51 :
52 0 : return samples;
53 : }
54 :
55 0 : int VorbisDecoder::decode_file(const std::string& filepath, std::vector<float>& output, unsigned int* channels, unsigned int* sample_rate)
56 : {
57 : int vorbis_error;
58 0 : stb_vorbis* decoder = stb_vorbis_open_filename(filepath.c_str(), &vorbis_error, NULL);
59 :
60 0 : if (!decoder) {
61 0 : return -1;
62 : }
63 :
64 0 : if (channels) {
65 0 : *channels = decoder->channels;
66 : }
67 :
68 0 : if (sample_rate) {
69 0 : *sample_rate = decoder->sample_rate;
70 : }
71 :
72 : float predata[1000];
73 0 : int total = 0;
74 :
75 0 : while (auto readed = stb_vorbis_get_samples_float_interleaved(decoder, decoder->channels, predata, 1000)) {
76 0 : output.resize(total + readed, 0.f);
77 0 : auto start = output.begin() + total;
78 0 : std::copy_n(predata, readed, start);
79 :
80 0 : total += readed;
81 0 : }
82 :
83 0 : stb_vorbis_close(decoder);
84 :
85 0 : return total;
86 : }
|