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"
33 #include <vorbis/codec.h>
34 #include <vorbis/vorbisfile.h>
37 #include "lisp/parser.hpp"
38 #include "lisp/lisp.hpp"
39 #include "file_system.hpp"
41 class SoundError : public std::exception
44 SoundError(const std::string& message) throw();
45 virtual ~SoundError() throw();
47 const char* what() const throw();
52 SoundError::SoundError(const std::string& message) throw()
54 this->message = message;
57 SoundError::~SoundError() throw()
61 SoundError::what() const throw()
63 return message.c_str();
66 class WavSoundFile : public SoundFile
69 WavSoundFile(PHYSFS_file* file);
72 size_t read(void* buffer, size_t buffer_size);
78 PHYSFS_sint64 datastart;
81 static inline uint32_t read32LE(PHYSFS_file* file)
84 if(PHYSFS_readULE32(file, &result) == 0)
85 throw SoundError("file too short");
90 static inline uint16_t read16LE(PHYSFS_file* file)
93 if(PHYSFS_readULE16(file, &result) == 0)
94 throw SoundError("file too short");
99 WavSoundFile::WavSoundFile(PHYSFS_file* file)
104 if(PHYSFS_read(file, magic, sizeof(magic), 1) != 1)
105 throw SoundError("Couldn't read file magic (not a wave file)");
106 if(strncmp(magic, "RIFF", 4) != 0) {
107 log_debug << "MAGIC: " << magic << std::endl;
108 throw SoundError("file is not a RIFF wav file");
111 uint32_t wavelen = read32LE(file);
114 if(PHYSFS_read(file, magic, sizeof(magic), 1) != 1)
115 throw SoundError("Couldn't read chunk header (not a wav file?)");
116 if(strncmp(magic, "WAVE", 4) != 0)
117 throw SoundError("file is not a valid RIFF/WAVE file");
122 // search audio data format chunk
124 if(PHYSFS_read(file, chunkmagic, sizeof(chunkmagic), 1) != 1)
125 throw SoundError("EOF while searching format chunk");
126 chunklen = read32LE(file);
128 if(strncmp(chunkmagic, "fmt ", 4) == 0)
131 if(strncmp(chunkmagic, "fact", 4) == 0
132 || strncmp(chunkmagic, "LIST", 4) == 0) {
134 if(PHYSFS_seek(file, PHYSFS_tell(file) + chunklen) == 0)
135 throw SoundError("EOF while searching fmt chunk");
137 throw SoundError("complex WAVE files not supported");
142 throw SoundError("Format chunk too short");
145 uint16_t encoding = read16LE(file);
147 throw SoundError("only PCM encoding supported");
148 channels = read16LE(file);
149 rate = read32LE(file);
150 uint32_t byterate = read32LE(file);
152 uint16_t blockalign = read16LE(file);
154 bits_per_sample = read16LE(file);
157 if(PHYSFS_seek(file, PHYSFS_tell(file) + (chunklen-16)) == 0)
158 throw SoundError("EOF while reading rest of format chunk");
161 // set file offset to DATA chunk data
163 if(PHYSFS_read(file, chunkmagic, sizeof(chunkmagic), 1) != 1)
164 throw SoundError("EOF while searching data chunk");
165 chunklen = read32LE(file);
167 if(strncmp(chunkmagic, "data", 4) == 0)
171 if(PHYSFS_seek(file, PHYSFS_tell(file) + chunklen) == 0)
172 throw SoundError("EOF while searching fmt chunk");
175 datastart = PHYSFS_tell(file);
176 size = static_cast<size_t> (chunklen);
179 WavSoundFile::~WavSoundFile()
185 WavSoundFile::reset()
187 if(PHYSFS_seek(file, datastart) == 0)
188 throw SoundError("Couldn't seek to data start");
192 WavSoundFile::read(void* buffer, size_t buffer_size)
194 PHYSFS_sint64 end = datastart + size;
195 PHYSFS_sint64 cur = PHYSFS_tell(file);
199 size_t readsize = std::min(static_cast<size_t> (end - cur), buffer_size);
200 if(PHYSFS_read(file, buffer, readsize, 1) != 1)
201 throw SoundError("read error while reading samples");
203 #ifdef WORDS_BIGENDIAN
204 if (bits_per_sample != 16)
206 char *tmp = (char*)buffer;
210 for (i = 0; i < readsize / 2; i++)
213 tmp[2*i] = tmp[2*i+1];
223 //---------------------------------------------------------------------------
225 class OggSoundFile : public SoundFile
228 OggSoundFile(PHYSFS_file* file, double loop_begin, double loop_at);
231 size_t read(void* buffer, size_t buffer_size);
235 static size_t cb_read(void* ptr, size_t size, size_t nmemb, void* source);
236 static int cb_seek(void* source, ogg_int64_t offset, int whence);
237 static int cb_close(void* source);
238 static long cb_tell(void* source);
241 OggVorbis_File vorbis_file;
242 ogg_int64_t loop_begin;
244 size_t normal_buffer_loop;
247 OggSoundFile::OggSoundFile(PHYSFS_file* file, double loop_begin, double loop_at)
251 ov_callbacks callbacks = { cb_read, cb_seek, cb_close, cb_tell };
252 ov_open_callbacks(file, &vorbis_file, 0, 0, callbacks);
254 vorbis_info* vi = ov_info(&vorbis_file, -1);
256 channels = vi->channels;
258 bits_per_sample = 16;
259 size = static_cast<size_t> (ov_pcm_total(&vorbis_file, -1) * 2);
261 double samples_begin = loop_begin * rate;
262 double sample_loop = loop_at * rate;
264 this->loop_begin = (ogg_int64_t) samples_begin;
266 this->loop_at = (ogg_int64_t) -1;
268 this->loop_at = (ogg_int64_t) sample_loop;
272 OggSoundFile::~OggSoundFile()
274 ov_clear(&vorbis_file);
278 OggSoundFile::read(void* _buffer, size_t buffer_size)
280 char* buffer = reinterpret_cast<char*> (_buffer);
282 size_t totalBytesRead = 0;
284 while(buffer_size>0) {
285 #ifdef WORDS_BIGENDIAN
291 size_t bytes_to_read = buffer_size;
293 size_t bytes_per_sample = 2;
294 ogg_int64_t time = ov_pcm_tell(&vorbis_file);
295 ogg_int64_t samples_left_till_loop = loop_at - time;
296 ogg_int64_t bytes_left_till_loop
297 = samples_left_till_loop * bytes_per_sample;
298 if(bytes_left_till_loop <= 4)
301 if(bytes_left_till_loop < (ogg_int64_t) bytes_to_read) {
302 bytes_to_read = (size_t) bytes_left_till_loop;
307 = ov_read(&vorbis_file, buffer, bytes_to_read, bigendian,
312 buffer_size -= bytesRead;
314 totalBytesRead += bytesRead;
317 return totalBytesRead;
321 OggSoundFile::reset()
323 ov_pcm_seek(&vorbis_file, loop_begin);
327 OggSoundFile::cb_read(void* ptr, size_t size, size_t nmemb, void* source)
329 PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);
332 = PHYSFS_read(file, ptr, static_cast<PHYSFS_uint32> (size),
333 static_cast<PHYSFS_uint32> (nmemb));
337 return static_cast<size_t> (res);
341 OggSoundFile::cb_seek(void* source, ogg_int64_t offset, int whence)
343 PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);
347 if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (offset)) == 0)
351 if(PHYSFS_seek(file, PHYSFS_tell(file) + offset) == 0)
355 if(PHYSFS_seek(file, PHYSFS_fileLength(file) + offset) == 0)
369 OggSoundFile::cb_close(void* source)
371 PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);
377 OggSoundFile::cb_tell(void* source)
379 PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);
380 return static_cast<long> (PHYSFS_tell(file));
383 //---------------------------------------------------------------------------
385 SoundFile* load_music_file(const std::string& filename)
387 lisp::Parser parser(false);
388 const lisp::Lisp* root = parser.parse(filename);
389 const lisp::Lisp* music = root->get_lisp("supertux-music");
391 throw SoundError("file is not a supertux-music file.");
393 std::string raw_music_file;
394 float loop_begin = 0;
397 music->get("file", raw_music_file);
398 music->get("loop-begin", loop_begin);
399 music->get("loop-at", loop_at);
402 throw SoundError("can't loop from negative value");
405 std::string basedir = FileSystem::dirname(filename);
406 raw_music_file = FileSystem::normalize(basedir + raw_music_file);
408 PHYSFS_file* file = PHYSFS_openRead(raw_music_file.c_str());
410 std::stringstream msg;
411 msg << "Couldn't open '" << raw_music_file << "': " << PHYSFS_getLastError();
412 throw SoundError(msg.str());
415 return new OggSoundFile(file, loop_begin, loop_at);
418 SoundFile* load_sound_file(const std::string& filename)
420 if(filename.length() > 6
421 && filename.compare(filename.length()-6, 6, ".music") == 0) {
422 return load_music_file(filename);
425 PHYSFS_file* file = PHYSFS_openRead(filename.c_str());
427 std::stringstream msg;
428 msg << "Couldn't open '" << filename << "': " << PHYSFS_getLastError() << ", using dummy sound file.";
429 throw SoundError(msg.str());
434 if(PHYSFS_read(file, magic, sizeof(magic), 1) != 1)
435 throw SoundError("Couldn't read magic, file too short");
436 PHYSFS_seek(file, 0);
437 if(strncmp(magic, "RIFF", 4) == 0)
438 return new WavSoundFile(file);
439 else if(strncmp(magic, "OggS", 4) == 0)
440 return new OggSoundFile(file, 0, -1);
442 throw SoundError("Unknown file format");
443 } catch(std::exception& e) {
444 std::stringstream msg;
445 msg << "Couldn't read '" << filename << "': " << e.what();
446 throw SoundError(msg.str());