Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/lib/sound/sound_buffer.cc @ 8625

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

orxonox/trunk: merged the osX-branche back here
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/osx . -r7763:HEAD

conflicts resolved, and everything is working as expected (or at least i hope so :) )

File size: 3.1 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#ifdef HAVE_SDL_SDL_H
25#include <SDL/SDL.h>
26#include <SDL/SDL_endian.h>
27#else
28#include <SDL.h>
29#include <SDL_endian.h>
30#endif
31namespace OrxSound
32{
33  //////////////////
34  /* SOUND-BUFFER */
35  //////////////////
36  /**
37   *  Creates a Soundbuffer out of an inputfile
38   * @param fileName The name of the File
39   */
40  SoundBuffer::SoundBuffer(const std::string& fileName)
41  {
42    this->setClassID(CL_SOUND_BUFFER, "SoundBuffer");
43    this->setName(fileName);
44
45    // generate a Buffer
46    alGenBuffers(1, &this->bufferID);
47    SoundEngine::checkError("Generate Buffer", __LINE__);
48    this->loadWAV(fileName);
49  }
50
51  SoundBuffer::~SoundBuffer()
52  {
53    //  SoundEngine::getInstance()->removeBuffer(this);
54    alDeleteBuffers(1, &this->bufferID);
55    SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__);
56  }
57
58  /**
59   * @brief loads a Waveform from the local fileSystem into this Source.
60   * @param fileName the Name of the File to Load.
61   * @returns true on success.
62   */
63  bool SoundBuffer::loadWAV(const std::string& fileName)
64  {
65    SDL_AudioSpec wavSpec;
66    Uint32 wavLength;
67    Uint8 *wavBuffer;
68
69    /* Load the WAV */
70    if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL)
71    {
72      PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError());
73      return false;
74    }
75#if SDL_BYTEORDER == SDL_BIG_ENDIAN
76                if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) ) {
77                        int cnt = wavLength/2;
78                        Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer;
79                        for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
80                                *wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
81                }
82#endif
83    alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec), 
84                        wavBuffer, wavLength, wavSpec.freq);
85                       
86    SDL_FreeWAV(wavBuffer);
87    if (SoundEngine::checkError("Could not load Wave file", __LINE__))
88      return true;
89    else
90      return false;
91  }
92
93  /**
94   * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator
95   * @param audiospec the AudioSpec to convert.
96   * @returns the AL_FORMAT
97   */
98  ALenum SoundBuffer::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
99  {
100    assert (audiospec != NULL);
101    bool stereo = true;
102    bool is16Bit = true;
103    if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8)
104      is16Bit = false;
105    if (audiospec->channels == 1)
106      stereo = false;
107
108    if (!stereo && !is16Bit)
109      return AL_FORMAT_MONO8;
110    else if (!stereo && is16Bit)
111      return AL_FORMAT_MONO16;
112    else if (stereo && !is16Bit)
113      return AL_FORMAT_STEREO8;
114    else if (stereo && is16Bit)
115      return AL_FORMAT_STEREO16;
116  }
117}
Note: See TracBrowser for help on using the repository browser.