Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/sound/sound_buffer_data.cc @ 9876

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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