Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/sound/sound_buffer_data.cc @ 9803

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

Ported SoundBuffer to match the Data-Paradigm

File size: 4.8 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_data.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  ObjectListDefinition(SoundBufferData);
38  //////////////////
39  /* SOUND-BUFFER */
40  //////////////////
41  /**
42   * @brief Creates a SoundbufferData out of an inputfile
43   * @param fileName The name of the File
44   */
45  SoundBufferData::SoundBufferData()
46  {
47    this->registerObject(this, SoundBufferData::_objectList);
48    this->bufferID = 0;
49
50    this->size = 0;
51    this->loop = AL_FALSE;
52
53
54  }
55
56  SoundBufferData::~SoundBufferData()
57  {
58    //  SoundEngine::getInstance()->removeBuffer(this);
59    alDeleteBuffers(1, &this->bufferID);
60    SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__);
61  }
62
63  /**
64   * @brief check the File-extension and loads either Wav of Ogg
65   * @param fileName the Name of the File to load.
66   * @returns true on success (file found, and loaded.)
67   */
68  bool SoundBufferData::load(const std::string& fileName)
69  {
70    // generate a Buffer
71    alGenBuffers(1, &this->bufferID);
72    SoundEngine::checkError("Generate Buffer", __LINE__);
73    if (!nocaseCmp(fileName.substr(fileName.size() - 3), "WAV"))
74    {
75      return this->loadWAV(fileName);
76    }
77    else if (!nocaseCmp(fileName.substr(fileName.size() - 3), "OGG"))
78      return this->loadOGG(fileName);
79    else
80      return false;
81  }
82
83  /**
84   * @brief loads a Waveform from the local fileSystem into this Source.
85   * @param fileName the Name of the File to Load.
86   * @returns true on success.
87   */
88  bool SoundBufferData::loadWAV(const std::string& fileName)
89  {
90    SDL_AudioSpec wavSpec;
91    Uint32 wavLength;
92    Uint8 *wavBuffer;
93
94    /* Load the WAV */
95    if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL)
96    {
97      PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError());
98      return false;
99    }
100#if SDL_BYTEORDER == SDL_BIG_ENDIAN
101    if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) )
102    {
103      int cnt = wavLength/2;
104      Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer;
105      for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
106        *wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
107    }
108#endif
109    alBufferData(this->bufferID, SoundBufferData::sdlAudioSpecToAlFormat(&wavSpec),
110                 wavBuffer, wavLength, wavSpec.freq);
111
112    SDL_FreeWAV(wavBuffer);
113    if (SoundEngine::checkError("Could not load Wave file", __LINE__))
114      return true;
115    else
116      return false;
117  }
118
119
120#ifndef AL_FORMAT_VORBIS_EXT
121#define AL_FORMAT_VORBIS_EXT 0x100030
122#endif
123  /**
124   * @brief loads an OGG-file into a SOundBuffer
125   * @param fileName the Name of the File to load.
126   * @returns true on success (file exists and is fully loaded), false otherwise.
127   */
128  bool SoundBufferData::loadOGG(const std::string& fileName)
129  {
130    void*     ovdata;
131    FILE*     fh;
132
133    fh = fopen( fileName.c_str() , "rb") ;
134    if( fh != NULL )
135    {
136      struct stat sbuf ;
137
138      if(stat( fileName.c_str(), &sbuf ) != -1)
139      {
140        ovdata = malloc(sbuf.st_size);
141        if(ovdata != NULL)
142        {
143          fread( ovdata, 1, sbuf.st_size, fh);
144
145          alBufferData( this->bufferID,
146                        AL_FORMAT_VORBIS_EXT,
147                        ovdata,
148                        sbuf.st_size,
149                        1) ;
150          SoundEngine::checkError("Could not load OGG file", __LINE__);
151
152          free(ovdata);
153        }
154        fclose(fh);
155      }
156      else
157        return false;
158    }
159    else
160      return false;
161
162    return true ;
163
164  }
165
166
167  /**
168   * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator
169   * @param audiospec the AudioSpec to convert.
170   * @returns the AL_FORMAT
171   */
172  ALenum SoundBufferData::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
173  {
174    assert (audiospec != NULL);
175    bool stereo = true;
176    bool is16Bit = true;
177    if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8)
178      is16Bit = false;
179    if (audiospec->channels == 1)
180      stereo = false;
181
182    if (!stereo && !is16Bit)
183      return AL_FORMAT_MONO8;
184    else if (!stereo && is16Bit)
185      return AL_FORMAT_MONO16;
186    else if (stereo && !is16Bit)
187      return AL_FORMAT_STEREO8;
188    else /* if (stereo && is16Bit) */
189      return AL_FORMAT_STEREO16;
190  }
191}
Note: See TracBrowser for help on using the repository browser.