4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /** Used SDL_mixer and glest source as reference */
23 #include "sound_file.hpp"
32 #include <vorbis/codec.h>
33 #include <vorbis/vorbisfile.h>
35 #include "lisp/parser.hpp"
36 #include "lisp/lisp.hpp"
37 #include "file_system.hpp"
39 class WavSoundFile : public SoundFile
42 WavSoundFile(PHYSFS_file* file);
45 size_t read(void* buffer, size_t buffer_size);
51 PHYSFS_sint64 datastart;
54 static inline uint32_t read32LE(PHYSFS_file* file)
57 if(PHYSFS_readULE32(file, &result) == 0)
58 throw std::runtime_error("file too short");
63 static inline uint16_t read16LE(PHYSFS_file* file)
66 if(PHYSFS_readULE16(file, &result) == 0)
67 throw std::runtime_error("file too short");
72 WavSoundFile::WavSoundFile(PHYSFS_file* file)
77 if(PHYSFS_read(file, magic, sizeof(magic), 1) != 1)
78 throw std::runtime_error("Couldn't read file magic (not a wave file)");
79 if(strncmp(magic, "RIFF", 4) != 0) {
80 log_debug << "MAGIC: " << magic << std::endl;
81 throw std::runtime_error("file is not a RIFF wav file");
84 uint32_t wavelen = read32LE(file);
87 if(PHYSFS_read(file, magic, sizeof(magic), 1) != 1)
88 throw std::runtime_error("Couldn't read chunk header (not a wav file?)");
89 if(strncmp(magic, "WAVE", 4) != 0)
90 throw std::runtime_error("file is not a valid RIFF/WAVE file");
95 // search audio data format chunk
97 if(PHYSFS_read(file, chunkmagic, sizeof(chunkmagic), 1) != 1)
98 throw std::runtime_error("EOF while searching format chunk");
99 chunklen = read32LE(file);
101 if(strncmp(chunkmagic, "fmt ", 4) == 0)
104 if(strncmp(chunkmagic, "fact", 4) == 0
105 || strncmp(chunkmagic, "LIST", 4) == 0) {
107 if(PHYSFS_seek(file, PHYSFS_tell(file) + chunklen) == 0)
108 throw std::runtime_error("EOF while searching fmt chunk");
110 throw std::runtime_error("complex WAVE files not supported");
115 throw std::runtime_error("Format chunk too short");
118 uint16_t encoding = read16LE(file);
120 throw std::runtime_error("only PCM encoding supported");
121 channels = read16LE(file);
122 rate = read32LE(file);
123 uint32_t byterate = read32LE(file);
125 uint16_t blockalign = read16LE(file);
127 bits_per_sample = read16LE(file);
130 if(PHYSFS_seek(file, PHYSFS_tell(file) + (chunklen-16)) == 0)
131 throw std::runtime_error("EOF while reading reast of format chunk");
134 // set file offset to DATA chunk data
136 if(PHYSFS_read(file, chunkmagic, sizeof(chunkmagic), 1) != 1)
137 throw std::runtime_error("EOF while searching data chunk");
138 chunklen = read32LE(file);
140 if(strncmp(chunkmagic, "data", 4) == 0)
144 if(PHYSFS_seek(file, PHYSFS_tell(file) + chunklen) == 0)
145 throw std::runtime_error("EOF while searching fmt chunk");
148 datastart = PHYSFS_tell(file);
149 size = static_cast<size_t> (chunklen);
152 WavSoundFile::~WavSoundFile()
158 WavSoundFile::reset()
160 if(PHYSFS_seek(file, datastart) == 0)
161 throw std::runtime_error("Couldn't seek to data start");
165 WavSoundFile::read(void* buffer, size_t buffer_size)
167 PHYSFS_sint64 end = datastart + size;
168 PHYSFS_sint64 cur = PHYSFS_tell(file);
172 size_t readsize = std::min(static_cast<size_t> (end - cur), buffer_size);
173 if(PHYSFS_read(file, buffer, readsize, 1) != 1)
174 throw std::runtime_error("read error while reading samples");
176 #ifdef WORDS_BIGENDIAN
177 if (bits_per_sample != 16)
179 char *tmp = (char*)buffer;
183 for (i = 0; i < readsize / 2; i++)
186 tmp[2*i] = tmp[2*i+1];
196 //---------------------------------------------------------------------------
198 class OggSoundFile : public SoundFile
201 OggSoundFile(PHYSFS_file* file, double loop_begin, double loop_at);
204 size_t read(void* buffer, size_t buffer_size);
208 static size_t cb_read(void* ptr, size_t size, size_t nmemb, void* source);
209 static int cb_seek(void* source, ogg_int64_t offset, int whence);
210 static int cb_close(void* source);
211 static long cb_tell(void* source);
214 OggVorbis_File vorbis_file;
215 ogg_int64_t loop_begin;
217 size_t normal_buffer_loop;
220 OggSoundFile::OggSoundFile(PHYSFS_file* file, double loop_begin, double loop_at)
224 ov_callbacks callbacks = { cb_read, cb_seek, cb_close, cb_tell };
225 ov_open_callbacks(file, &vorbis_file, 0, 0, callbacks);
227 vorbis_info* vi = ov_info(&vorbis_file, -1);
229 channels = vi->channels;
231 bits_per_sample = 16;
232 size = static_cast<size_t> (ov_pcm_total(&vorbis_file, -1) * 2);
234 double sample_len = 1.0f / rate;
235 double samples_begin = loop_begin / sample_len;
236 double sample_loop = loop_at / sample_len;
238 this->loop_begin = (ogg_int64_t) samples_begin;
240 this->loop_at = (ogg_int64_t) -1;
242 this->loop_at = (ogg_int64_t) sample_loop;
246 OggSoundFile::~OggSoundFile()
248 ov_clear(&vorbis_file);
252 OggSoundFile::read(void* _buffer, size_t buffer_size)
254 char* buffer = reinterpret_cast<char*> (_buffer);
256 size_t totalBytesRead = 0;
258 while(buffer_size>0) {
259 #ifdef WORDS_BIGENDIAN
265 size_t bytes_to_read = buffer_size;
267 size_t bytes_per_sample = 2;
268 ogg_int64_t time = ov_pcm_tell(&vorbis_file);
269 ogg_int64_t samples_left_till_loop = loop_at - time;
270 ogg_int64_t bytes_left_till_loop
271 = samples_left_till_loop * bytes_per_sample;
272 if(bytes_left_till_loop <= 4)
275 if(bytes_left_till_loop < (ogg_int64_t) bytes_to_read) {
276 bytes_to_read = (size_t) bytes_left_till_loop;
281 = ov_read(&vorbis_file, buffer, bytes_to_read, bigendian,
286 buffer_size -= bytesRead;
288 totalBytesRead += bytesRead;
291 return totalBytesRead;
295 OggSoundFile::reset()
297 ov_pcm_seek(&vorbis_file, loop_begin);
301 OggSoundFile::cb_read(void* ptr, size_t size, size_t nmemb, void* source)
303 PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);
306 = PHYSFS_read(file, ptr, static_cast<PHYSFS_uint32> (size),
307 static_cast<PHYSFS_uint32> (nmemb));
311 return static_cast<size_t> (res);
315 OggSoundFile::cb_seek(void* source, ogg_int64_t offset, int whence)
317 PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);
321 if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (offset)) == 0)
325 if(PHYSFS_seek(file, PHYSFS_tell(file) + offset) == 0)
329 if(PHYSFS_seek(file, PHYSFS_fileLength(file) + offset) == 0)
343 OggSoundFile::cb_close(void* source)
345 PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);
351 OggSoundFile::cb_tell(void* source)
353 PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);
354 return static_cast<long> (PHYSFS_tell(file));
357 //---------------------------------------------------------------------------
359 SoundFile* load_music_file(const std::string& filename)
361 lisp::Parser parser(false);
362 const lisp::Lisp* root = parser.parse(filename);
363 const lisp::Lisp* music = root->get_lisp("supertux-music");
365 throw std::runtime_error("file is not a supertux-music file.");
367 std::string raw_music_file;
368 float loop_begin = 0;
371 music->get("file", raw_music_file);
372 music->get("loop-begin", loop_begin);
373 music->get("loop-at", loop_at);
376 throw std::runtime_error("can't loop from negative value");
379 std::string basedir = FileSystem::dirname(filename);
380 raw_music_file = FileSystem::normalize(basedir + raw_music_file);
382 PHYSFS_file* file = PHYSFS_openRead(raw_music_file.c_str());
384 std::stringstream msg;
385 msg << "Couldn't open '" << raw_music_file << "': " << PHYSFS_getLastError();
386 throw std::runtime_error(msg.str());
389 return new OggSoundFile(file, loop_begin, loop_at);
392 SoundFile* load_sound_file(const std::string& filename)
394 if(filename.length() > 6
395 && filename.compare(filename.length()-6, 6, ".music") == 0) {
396 return load_music_file(filename);
399 PHYSFS_file* file = PHYSFS_openRead(filename.c_str());
401 std::stringstream msg;
402 msg << "Couldn't open '" << filename << "': " << PHYSFS_getLastError();
403 throw std::runtime_error(msg.str());
408 if(PHYSFS_read(file, magic, sizeof(magic), 1) != 1)
409 throw std::runtime_error("Couldn't read magic, file too short");
410 PHYSFS_seek(file, 0);
411 if(strncmp(magic, "RIFF", 4) == 0)
412 return new WavSoundFile(file);
413 else if(strncmp(magic, "OggS", 4) == 0)
414 return new OggSoundFile(file, 0, -1);
416 throw std::runtime_error("Unknown file format");
417 } catch(std::exception& e) {
418 std::stringstream msg;
419 msg << "Couldn't read '" << filename << "': " << e.what();
420 throw std::runtime_error(msg.str());