Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / audio / sound_file.cpp
index 1420a17..01a4032 100644 (file)
@@ -1,16 +1,37 @@
+//  $Id$
+//
+//  SuperTux
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
 /** Used SDL_mixer and glest source as reference */
 #include <config.h>
 
-#include "sound_file.h"
+#include "sound_file.hpp"
 
 #include <stdio.h>
 #include <stdint.h>
 #include <algorithm>
 #include <stdexcept>
 #include <sstream>
+#include <assert.h>
 #include <physfs.h>
 #include <vorbis/codec.h>
 #include <vorbis/vorbisfile.h>
+#include "log.hpp"
 
 class WavSoundFile : public SoundFile
 {
@@ -53,7 +74,7 @@ WavSoundFile::WavSoundFile(PHYSFS_file* file)
   if(PHYSFS_read(file, magic, sizeof(magic), 1) != 1)
     throw std::runtime_error("Couldn't read file magic (not a wave file)");
   if(strncmp(magic, "RIFF", 4) != 0) {
-    printf("MAGIC: %4s.\n", magic);
+    log_debug << "MAGIC: " << magic << std::endl;
     throw std::runtime_error("file is not a RIFF wav file");
   }
 
@@ -149,6 +170,23 @@ WavSoundFile::read(void* buffer, size_t buffer_size)
   if(PHYSFS_read(file, buffer, readsize, 1) != 1)
     throw std::runtime_error("read error while reading samples");
 
+#ifdef WORDS_BIGENDIAN
+  if (bits_per_sample != 16)
+    return readsize;
+  char *tmp = (char*)buffer;
+
+  size_t i;
+  char c;
+  for (i = 0; i < readsize / 2; i++)
+  {
+    c          = tmp[2*i];
+    tmp[2*i]   = tmp[2*i+1];
+    tmp[2*i+1] = c;
+  }
+
+  buffer = tmp;
+#endif
+
   return readsize;
 }
 
@@ -201,8 +239,13 @@ OggSoundFile::read(void* _buffer, size_t buffer_size)
 
   while(buffer_size>0){
     long bytesRead 
-      = ov_read(&vorbis_file, buffer, static_cast<int> (buffer_size), 0, 2, 1,
-          &section);
+      = ov_read(&vorbis_file, buffer, static_cast<int> (buffer_size),
+#ifdef WORDS_BIGENDIAN
+1,
+#else
+0,
+#endif
+          2, 1, &section);
     if(bytesRead==0){
       break;
     }