1 /*
2 ** sounddecoder.cpp
3 ** baseclass for sound format decoders
4 **
5 **---------------------------------------------------------------------------
6 ** Copyright 2008-2019 Chris Robinson
7 ** All rights reserved.
8 **
9 ** Redistribution and use in source and binary forms, with or without
10 ** modification, are permitted provided that the following conditions
11 ** are met:
12 **
13 ** 1. Redistributions of source code must retain the above copyright
14 **    notice, this list of conditions and the following disclaimer.
15 ** 2. Redistributions in binary form must reproduce the above copyright
16 **    notice, this list of conditions and the following disclaimer in the
17 **    documentation and/or other materials provided with the distribution.
18 ** 3. The name of the author may not be used to endorse or promote products
19 **    derived from this software without specific prior written permission.
20 **
21 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 **---------------------------------------------------------------------------
32 **
33 */
34 
35 
36 #include "zmusic/zmusic_internal.h"
37 #include "sndfile_decoder.h"
38 #include "mpg123_decoder.h"
39 
CreateDecoder(MusicIO::FileInterface * reader)40 SoundDecoder *SoundDecoder::CreateDecoder(MusicIO::FileInterface *reader)
41 {
42     SoundDecoder *decoder = NULL;
43     auto pos = reader->tell();
44 
45 #ifdef HAVE_SNDFILE
46 		decoder = new SndFileDecoder;
47 		if (decoder->open(reader))
48 			return decoder;
49 		reader->seek(pos, SEEK_SET);
50 
51 		delete decoder;
52 		decoder = NULL;
53 #endif
54 #ifdef HAVE_MPG123
55 		decoder = new MPG123Decoder;
56 		if (decoder->open(reader))
57 			return decoder;
58 		reader->seek(pos, SEEK_SET);
59 
60 		delete decoder;
61 		decoder = NULL;
62 #endif
63     return decoder;
64 }
65 
66 
67 // Default readAll implementation, for decoders that can't do anything better
readAll()68 std::vector<uint8_t> SoundDecoder::readAll()
69 {
70     std::vector<uint8_t> output;
71     unsigned total = 0;
72     unsigned got;
73 
74     output.resize(total+32768);
75     while((got=(unsigned)read((char*)&output[total], output.size()-total)) > 0)
76     {
77         total += got;
78         output.resize(total*2);
79     }
80     output.resize(total);
81     return output;
82 }
83 
84 //==========================================================================
85 //
86 // other callbacks
87 //
88 //==========================================================================
89 extern "C"
dumb_decode_vorbis(int outlen,const void * oggstream,int sizebytes)90 short* dumb_decode_vorbis(int outlen, const void* oggstream, int sizebytes)
91 {
92 	short* samples = (short*)calloc(1, outlen);
93 	ChannelConfig chans;
94 	SampleType type;
95 	int srate;
96 
97 	// The decoder will take ownership of the reader if it succeeds so this may not be a local variable.
98 	MusicIO::MemoryReader* reader = new MusicIO::MemoryReader((const uint8_t*)oggstream, sizebytes);
99 
100 	SoundDecoder* decoder = SoundDecoder::CreateDecoder(reader);
101 	if (!decoder)
102 	{
103 		reader->close();
104 		return samples;
105 	}
106 
107 	decoder->getInfo(&srate, &chans, &type);
108 	if (chans != ChannelConfig_Mono || type != SampleType_Int16)
109 	{
110 		delete decoder;
111 		return samples;
112 	}
113 
114 	decoder->read((char*)samples, outlen);
115 	delete decoder;
116 	return samples;
117 }
118 
CreateDecoder(const uint8_t * data,size_t size,zmusic_bool isstatic)119 DLL_EXPORT struct SoundDecoder* CreateDecoder(const uint8_t* data, size_t size, zmusic_bool isstatic)
120 {
121 	MusicIO::FileInterface* reader;
122 	if (isstatic) reader = new MusicIO::MemoryReader(data, (long)size);
123 	else reader = new MusicIO::VectorReader(data, size);
124 	auto res = SoundDecoder::CreateDecoder(reader);
125 	if (!res) reader->close();
126 	return res;
127 }
128 
SoundDecoder_GetInfo(struct SoundDecoder * decoder,int * samplerate,ChannelConfig * chans,SampleType * type)129 DLL_EXPORT void SoundDecoder_GetInfo(struct SoundDecoder* decoder, int* samplerate, ChannelConfig* chans, SampleType* type)
130 {
131 	if (decoder) decoder->getInfo(samplerate, chans, type);
132 	else if (samplerate) *samplerate = 0;
133 }
134 
SoundDecoder_Read(struct SoundDecoder * decoder,void * buffer,size_t length)135 DLL_EXPORT size_t SoundDecoder_Read(struct SoundDecoder* decoder, void* buffer, size_t length)
136 {
137 	if (decoder) return decoder->read((char*)buffer, length);
138 	else return 0;
139 }
140 
SoundDecoder_Close(struct SoundDecoder * decoder)141 DLL_EXPORT void SoundDecoder_Close(struct SoundDecoder* decoder)
142 {
143 	if (decoder) delete decoder;
144 }
145