Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/sound/sound_buffer.cc @ 9767

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

renamed newclassid to classid and newobjectlist to objectlist

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