1/**
2 * Copyright (c) 2006-2023 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
28namespace love
29{
30namespace sound
31{
32namespace lullaby
33{
34
35GmeDecoder::GmeDecoder(Data *data, int bufferSize)
36 : Decoder(data, 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
64GmeDecoder::~GmeDecoder()
65{
66 if (emu)
67 gme_delete(emu);
68}
69
70bool 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
87love::sound::Decoder *GmeDecoder::clone()
88{
89 return new GmeDecoder(data.get(), bufferSize);
90}
91
92int 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
112bool GmeDecoder::seek(double s)
113{
114 return gme_seek(emu, static_cast<long>(s * 1000.0)) != 0;
115}
116
117bool 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
130bool GmeDecoder::isSeekable()
131{
132 return true;
133}
134
135int GmeDecoder::getChannelCount() const
136{
137 return 2;
138}
139
140int GmeDecoder::getBitDepth() const
141{
142 return 16;
143}
144
145double GmeDecoder::getDuration()
146{
147 return -1;
148}
149
150} // lullaby
151} // sound
152} // love
153
154#endif // LOVE_SUPPORT_GME
155