Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/sound/sound_buffer.cc @ 8581

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

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

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