1 /**
2  * Copyright (c) 2006-2016 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 #include "common/config.h"
22 
23 #ifdef LOVE_SUPPORT_GME
24 
25 #include "common/Exception.h"
26 #include "GmeDecoder.h"
27 
28 namespace love
29 {
30 namespace sound
31 {
32 namespace lullaby
33 {
34 
GmeDecoder(Data * data,const std::string & ext,int bufferSize)35 GmeDecoder::GmeDecoder(Data *data, const std::string &ext, int bufferSize)
36 	: Decoder(data, ext, bufferSize)
37 	, emu(0)
38 	, num_tracks(0)
39 	, cur_track(0)
40 {
41 	void *d = data->getData();
42 	int s = data->getSize();
43 
44 	if (gme_open_data(d, s, &emu, sampleRate) != 0)
45 		throw love::Exception("Could not open game music file");
46 
47 	num_tracks = gme_track_count(emu);
48 
49 	try
50 	{
51 		if (num_tracks <= 0)
52 			throw love::Exception("Game music file has no tracks");
53 
54 		if (gme_start_track(emu, cur_track) != 0)
55 			throw love::Exception("Could not start game music playback");
56 	}
57 	catch (love::Exception &)
58 	{
59 		gme_delete(emu);
60 		throw;
61 	}
62 }
63 
~GmeDecoder()64 GmeDecoder::~GmeDecoder()
65 {
66 	if (emu)
67 		gme_delete(emu);
68 }
69 
accepts(const std::string & ext)70 bool GmeDecoder::accepts(const std::string &ext)
71 {
72 	static const std::string supported[] =
73 	{
74 		"ay", "gbs", "gym", "hes", "kss", "nsf",
75 		"nsfe", "sap", "spc", "vgm", "vgz", ""
76 	};
77 
78 	for (int i = 0; !(supported[i].empty()); i++)
79 	{
80 		if (supported[i].compare(ext) == 0)
81 			return true;
82 	}
83 
84 	return false;
85 }
86 
clone()87 love::sound::Decoder *GmeDecoder::clone()
88 {
89 	return new GmeDecoder(data.get(), ext, bufferSize);
90 }
91 
decode()92 int GmeDecoder::decode()
93 {
94 	short *sbuf = static_cast<short*>(buffer);
95 	int size = bufferSize / sizeof(short);
96 
97 	if (gme_play(emu, size, sbuf) != 0)
98 		throw love::Exception("Error while decoding game music");
99 
100 	if (!eof && gme_track_ended(emu))
101 	{
102 		// Start the next track if this one ended.
103 		if (cur_track < num_tracks - 1)
104 			gme_start_track(emu, ++cur_track);
105 		else
106 			eof = true;
107 	}
108 
109 	return bufferSize;
110 }
111 
seek(float s)112 bool GmeDecoder::seek(float s)
113 {
114 	return gme_seek(emu, static_cast<long>(s * 1000.f)) != 0;
115 }
116 
rewind()117 bool GmeDecoder::rewind()
118 {
119 	// If we're in the first track, rewind.
120 	if (cur_track == 0)
121 		return gme_seek(emu, 0) == 0;
122 	else
123 	{
124 		// Otherwise, start from the first track again.
125 		cur_track = 0;
126 		return gme_start_track(emu, cur_track) == 0;
127 	}
128 }
129 
isSeekable()130 bool GmeDecoder::isSeekable()
131 {
132 	return true;
133 }
134 
getChannels() const135 int GmeDecoder::getChannels() const
136 {
137 	return 2;
138 }
139 
getBitDepth() const140 int GmeDecoder::getBitDepth() const
141 {
142 	return 16;
143 }
144 
getDuration()145 double GmeDecoder::getDuration()
146 {
147 	return -1;
148 }
149 
150 } // lullaby
151 } // sound
152 } // love
153 
154 #endif // LOVE_SUPPORT_GME
155