Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/sound/sound_buffer.cc @ 9408

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

trunk: warning message, if ogg loading fails

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