Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/sound/sound_buffer.cc @ 9423

Last change on this file since 9423 was 8971, checked in by bensch, 18 years ago

trunk: warning message, if ogg loading fails

File size: 4.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND
17
18#include "sound_buffer.h"
19
20#include "sound_engine.h"
21
22#include "sdlincl.h"
23#include <cassert>
24#include "debug.h"
25#include "sys/stat.h"
26#include "helper_functions.h"
27
28#ifdef HAVE_SDL_SDL_H
29#include <SDL/SDL.h>
30#include <SDL/SDL_endian.h>
31#else
32#include <SDL.h>
33#include <SDL_endian.h>
34#endif
35namespace OrxSound
36{
37  //////////////////
38  /* SOUND-BUFFER */
39  //////////////////
40  /**
41   *  Creates a Soundbuffer out of an inputfile
42   * @param fileName The name of the File
43   */
44  SoundBuffer::SoundBuffer(const std::string& fileName)
45  {
46    this->setClassID(CL_SOUND_BUFFER, "SoundBuffer");
47    this->setName(fileName);
48
49    // generate a Buffer
50    alGenBuffers(1, &this->bufferID);
51    SoundEngine::checkError("Generate Buffer", __LINE__);
52    if (!nocaseCmp(fileName.substr(fileName.size() - 3), "WAV"))
53    {
54      this->loadWAV(fileName);
55    }
56    else if (!nocaseCmp(fileName.substr(fileName.size() - 3), "OGG"))
57      this->loadOGG(fileName);
58
59  }
60
61  SoundBuffer::~SoundBuffer()
62  {
63    //  SoundEngine::getInstance()->removeBuffer(this);
64    alDeleteBuffers(1, &this->bufferID);
65    SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__);
66  }
67
68  /**
69   * @brief loads a Waveform from the local fileSystem into this Source.
70   * @param fileName the Name of the File to Load.
71   * @returns true on success.
72   */
73  bool SoundBuffer::loadWAV(const std::string& fileName)
74  {
75    SDL_AudioSpec wavSpec;
76    Uint32 wavLength;
77    Uint8 *wavBuffer;
78
79    /* Load the WAV */
80    if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL)
81    {
82      PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError());
83      return false;
84    }
85#if SDL_BYTEORDER == SDL_BIG_ENDIAN
86    if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) )
87    {
88      int cnt = wavLength/2;
89      Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer;
90      for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
91        *wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
92    }
93#endif
94    alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec),
95                 wavBuffer, wavLength, wavSpec.freq);
96
97    SDL_FreeWAV(wavBuffer);
98    if (SoundEngine::checkError("Could not load Wave file", __LINE__))
99      return true;
100    else
101      return false;
102  }
103
104
105#ifndef AL_FORMAT_VORBIS_EXT
106#define AL_FORMAT_VORBIS_EXT 0x100030
107#endif
108  /**
109   * @brief loads an OGG-file into a SOundBuffer
110   * @param fileName the Name of the File to load.
111   * @returns true on success (file exists and is fully loaded), false otherwise.
112   */
113  bool SoundBuffer::loadOGG(const std::string& fileName)
114  {
115    void*     ovdata;
116    FILE*     fh;
117
118    fh = fopen( fileName.c_str() , "rb") ;
119    if( fh != NULL )
120    {
121      struct stat sbuf ;
122
123      if(stat( fileName.c_str(), &sbuf ) != -1)
124      {
125        ovdata = malloc(sbuf.st_size);
126        if(ovdata != NULL)
127        {
128          fread( ovdata, 1, sbuf.st_size, fh);
129
130          alBufferData( this->bufferID,
131                        AL_FORMAT_VORBIS_EXT,
132                        ovdata,
133                        sbuf.st_size,
134                        1) ;
135          SoundEngine::checkError("Could not load OGG file", __LINE__);
136
137          free(ovdata);
138        }
139        fclose(fh);
140      }
141      else
142        return false;
143    }
144    else
145      return false;
146
147    return true ;
148
149  }
150
151
152  /**
153   * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator
154   * @param audiospec the AudioSpec to convert.
155   * @returns the AL_FORMAT
156   */
157  ALenum SoundBuffer::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
158  {
159    assert (audiospec != NULL);
160    bool stereo = true;
161    bool is16Bit = true;
162    if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8)
163      is16Bit = false;
164    if (audiospec->channels == 1)
165      stereo = false;
166
167    if (!stereo && !is16Bit)
168      return AL_FORMAT_MONO8;
169    else if (!stereo && is16Bit)
170      return AL_FORMAT_MONO16;
171    else if (stereo && !is16Bit)
172      return AL_FORMAT_STEREO8;
173    else /* if (stereo && is16Bit) */
174      return AL_FORMAT_STEREO16;
175  }
176}
Note: See TracBrowser for help on using the repository browser.