# HG changeset patch # User koda # Date 1255558322 0 # Node ID fececcbc218986f15274840d919632d5ceec5019 # Parent 228757f6c54d6b99d1370ea8ac7618c6791b8ebb Smaxx patch for fixing all sound related issues on any system diff -r 228757f6c54d -r fececcbc2189 QTfrontend/CMakeLists.txt --- a/QTfrontend/CMakeLists.txt Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/CMakeLists.txt Wed Oct 14 22:12:02 2009 +0000 @@ -20,8 +20,12 @@ include_directories(.) include_directories(${SDL_INCLUDE_DIR}) +include_directories(${OPENAL_INCLUDE_DIR}) +include_directories(${PROJECT_SOURCE_DIR}/openalbridge/) + if(UNIX) - include_directories("/usr/local/include") # HACK: in freebsd cannot find iconv.h included via SDL.h + # HACK: in freebsd cannot find iconv.h included via SDL.h + include_directories("/usr/local/include") endif(UNIX) diff -r 228757f6c54d -r fececcbc2189 QTfrontend/SDLs.cpp --- a/QTfrontend/SDLs.cpp Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/SDLs.cpp Wed Oct 14 22:12:02 2009 +0000 @@ -28,13 +28,17 @@ extern char xb360dpad[128]; extern char xbox360axes[][128]; +#ifdef _WIN32 bool hardware; +#endif extern char *programname; SDLInteraction::SDLInteraction() { music = -1; +#ifdef _WIN32 hardware = false; +#endif SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); @@ -46,13 +50,15 @@ SDLInteraction::~SDLInteraction() { SDL_Quit(); - openal_close(); + oalb_close(); } +#ifdef _WIN32 void SDLInteraction::setHardwareSound(bool hardware_snd) { hardware = hardware_snd; } +#endif QStringList SDLInteraction::getResolutions() const { @@ -68,7 +74,7 @@ } else { for(int i = 0; modes[i]; ++i) - if ((modes[i]->w >= 640) && (modes[i]->h >= 480)) + if ((modes[i]->w >= 640) && (modes[i]->h >= 480) && (modes[i]->h <= 1200)) result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h); } @@ -160,24 +166,28 @@ { OpenAL_Init(); if (music < 0) { - music = openal_loadfile(QString(datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData()); - openal_toggleloop(music); + music = oalb_loadfile(QString(datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData()); } - openal_setvolume(music, 60); - openal_fadein(music, 30); + oalb_playsound(music, 1); + oalb_setvolume(music, 60); } void SDLInteraction::StopMusic() { - if (music >= 0) openal_fadeout(music, 40); +// if (music >= 0) openal_fadeout(music, 40); + oalb_stopsound(music); } //we need thjs wrapper because of some issues with windows drivers //beware that this cause a slight delay when playing the first sound void OpenAL_Init() { - if (!openal_ready()) - openal_init(programname, hardware ? 1 : 0, 5); + if (!oalb_ready()) +#ifdef _WIN32 + oalb_init(programname, hardware ? 1 : 0); +#else + oalb_init(programname, 0); +#endif } diff -r 228757f6c54d -r fececcbc2189 QTfrontend/SDLs.h --- a/QTfrontend/SDLs.h Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/SDLs.h Wed Oct 14 22:12:02 2009 +0000 @@ -20,21 +20,7 @@ #define SDLS_H #include - -extern "C" bool openal_init (char *programname, bool usehardware, unsigned int memorysize); -extern "C" bool openal_close (void); -extern "C" bool openal_ready (void); -extern "C" int openal_loadfile (const char *filename); -extern "C" bool openal_toggleloop (unsigned int index); -extern "C" bool openal_setvolume (unsigned int index, unsigned char percentage); -extern "C" bool openal_setglobalvolume (unsigned char percentage); -extern "C" bool openal_togglemute (void); -extern "C" bool openal_fadeout (unsigned int index, unsigned short int quantity); -extern "C" bool openal_fadein (unsigned int index, unsigned short int quantity); -extern "C" bool openal_fade (unsigned int index, unsigned short int quantity, bool direction); -extern "C" bool openal_playsound (unsigned int index); -extern "C" bool openal_stopsound (unsigned int index); -extern "C" bool openal_pausesound (unsigned int index); +#include "openalbridge.h" class SDLInteraction : public QObject { diff -r 228757f6c54d -r fececcbc2189 QTfrontend/game.cpp --- a/QTfrontend/game.cpp Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/game.cpp Wed Oct 14 22:12:02 2009 +0000 @@ -283,7 +283,11 @@ arguments << QString("%1").arg(ipc_port); arguments << (config->vid_Fullscreen() ? "1" : "0"); arguments << (config->isSoundEnabled() ? "1" : "0"); +#ifdef _WIN32 arguments << (config->isSoundHardware() ? "1" : "0"); +#else + arguments << "0"; +#endif arguments << tr("en.txt"); arguments << QString::number(config->volume()); // sound volume arguments << QString::number(config->timerInterval()); diff -r 228757f6c54d -r fececcbc2189 QTfrontend/gameuiconfig.cpp --- a/QTfrontend/gameuiconfig.cpp Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/gameuiconfig.cpp Wed Oct 14 22:12:02 2009 +0000 @@ -48,7 +48,9 @@ Form->ui.pageOptions->CBReduceQuality->setChecked(value("video/reducequality", false).toBool()); Form->ui.pageOptions->CBFrontendEffects->setChecked(frontendEffects); Form->ui.pageOptions->CBEnableSound->setChecked(value("audio/sound", true).toBool()); +#ifdef _WIN32 Form->ui.pageOptions->CBHardwareSound->setChecked(value("audio/hardware", false).toBool()); +#endif Form->ui.pageOptions->CBEnableMusic->setChecked(value("audio/music", true).toBool()); Form->ui.pageOptions->volumeBox->setValue(value("audio/volume", 100).toUInt()); @@ -120,7 +122,9 @@ } setValue("audio/sound", isSoundEnabled()); +#ifdef _WIN32 setValue("audio/hardware", isSoundHardware()); +#endif setValue("audio/music", isMusicEnabled()); setValue("audio/volume", Form->ui.pageOptions->volumeBox->value()); @@ -177,10 +181,12 @@ return Form->ui.pageOptions->CBEnableSound->isChecked(); } +#ifdef _WIN32 bool GameUIConfig::isSoundHardware() { return Form->ui.pageOptions->CBHardwareSound->isChecked(); } +#endif bool GameUIConfig::isMusicEnabled() { diff -r 228757f6c54d -r fececcbc2189 QTfrontend/gameuiconfig.h --- a/QTfrontend/gameuiconfig.h Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/gameuiconfig.h Wed Oct 14 22:12:02 2009 +0000 @@ -36,7 +36,9 @@ QRect vid_Resolution(); bool vid_Fullscreen(); bool isSoundEnabled(); +#ifdef _WIN32 bool isSoundHardware(); +#endif bool isMusicEnabled(); bool isShowFPSEnabled(); bool isAltDamageEnabled(); diff -r 228757f6c54d -r fececcbc2189 QTfrontend/hwform.cpp --- a/QTfrontend/hwform.cpp Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/hwform.cpp Wed Oct 14 22:12:02 2009 +0000 @@ -77,7 +77,9 @@ CustomizePalettes(); +#ifdef _WIN32 sdli.setHardwareSound(settings.value("audio/hardware", false).toBool()); +#endif ui.pageOptions->CBResolution->addItems(sdli.getResolutions()); diff -r 228757f6c54d -r fececcbc2189 QTfrontend/pages.cpp --- a/QTfrontend/pages.cpp Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/pages.cpp Wed Oct 14 22:12:02 2009 +0000 @@ -195,7 +195,7 @@ } hbox->addWidget(CBVoicepack, 100); BtnTestSound = addButton(":/res/PlaySound.png", hbox, 1, true); - //BtnTestSound->setEnabled(openal_ready()); + //BtnTestSound->setEnabled(oalb_ready()); hbox->setStretchFactor(BtnTestSound, 1); connect(BtnTestSound, SIGNAL(clicked()), this, SLOT(testSound())); GBTLayout->addLayout(hbox); @@ -300,8 +300,8 @@ tmpdir.cd(CBVoicepack->currentText()); QStringList list = tmpdir.entryList(QStringList() << "Illgetyou.ogg" << "Incoming.ogg" << "Stupid.ogg" << "Coward.ogg" << "Firstblood.ogg", QDir::Files); if (list.size()) { - sound = openal_loadfile(QString(tmpdir.absolutePath() + "/" + list[rand() % list.size()]).toLocal8Bit().constData()); - openal_playsound(sound); + sound = oalb_loadfile(QString(tmpdir.absolutePath() + "/" + list[rand() % list.size()]).toLocal8Bit().constData()); + oalb_playsound(sound, 0); } } @@ -431,18 +431,20 @@ CBReduceQuality->setText(QCheckBox::tr("Reduced quality")); GBAlayout->addWidget(CBReduceQuality); +#ifdef _WIN32 CBHardwareSound = new QCheckBox(AGGroupBox); CBHardwareSound->setText(QCheckBox::tr("Hardware sound (if available; requires restart)")); - //CBHardwareSound->setEnabled(openal_ready()); + //CBHardwareSound->setEnabled(oalb_ready()); GBAlayout->addWidget(CBHardwareSound); +#endif CBEnableSound = new QCheckBox(AGGroupBox); CBEnableSound->setText(QCheckBox::tr("Enable sound")); - //CBEnableSound->setEnabled(openal_ready()); + //CBEnableSound->setEnabled(oalb_ready()); GBAlayout->addWidget(CBEnableSound); CBEnableMusic = new QCheckBox(AGGroupBox); CBEnableMusic->setText(QCheckBox::tr("Enable music")); - //CBEnableMusic->setEnabled(openal_ready()); + //CBEnableMusic->setEnabled(oalb_ready()); GBAlayout->addWidget(CBEnableMusic); QHBoxLayout * GBAvollayout = new QHBoxLayout(0); @@ -453,7 +455,7 @@ volumeBox = new QSpinBox(AGGroupBox); volumeBox->setRange(0, 100); volumeBox->setSingleStep(5); - //volumeBox->setEnabled(openal_ready()); + //volumeBox->setEnabled(oalb_ready()); GBAvollayout->addWidget(volumeBox); CBShowFPS = new QCheckBox(AGGroupBox); @@ -789,7 +791,6 @@ pageLayout->addLayout(newRoomLayout, 0, 0); roomsList = new QTableWidget(this); - roomsList->setColumnCount(7); roomsList->setSelectionBehavior(QAbstractItemView::SelectRows); roomsList->verticalHeader()->setVisible(false); roomsList->horizontalHeader()->setResizeMode(QHeaderView::Interactive); @@ -822,6 +823,7 @@ void PageRoomsList::setRoomsList(const QStringList & list) { roomsList->clear(); + roomsList->setColumnCount(7); roomsList->setHorizontalHeaderLabels( QStringList() << QTableWidget::tr("Room Name") << @@ -903,7 +905,7 @@ roomsList->setItem(r, 6, item); } - roomsList->resizeColumnsToContents(); + //roomsList->resizeColumnsToContents(); } void PageRoomsList::onCreateClick() diff -r 228757f6c54d -r fececcbc2189 QTfrontend/pages.h --- a/QTfrontend/pages.h Wed Oct 14 16:30:41 2009 +0000 +++ b/QTfrontend/pages.h Wed Oct 14 22:12:02 2009 +0000 @@ -208,7 +208,9 @@ IconedGroupBox *AGGroupBox; QComboBox *CBResolution; QCheckBox *CBEnableSound; +#ifdef _WIN32 QCheckBox *CBHardwareSound; +#endif QCheckBox *CBEnableMusic; QCheckBox *CBFullscreen; QCheckBox *CBFrontendFullscreen; diff -r 228757f6c54d -r fececcbc2189 hedgewars/uSound.pas --- a/hedgewars/uSound.pas Wed Oct 14 16:30:41 2009 +0000 +++ b/hedgewars/uSound.pas Wed Oct 14 22:12:02 2009 +0000 @@ -58,20 +58,16 @@ function AskForVoicepack(name: shortstring): PVoicepack; function soundFadeOut(snd: TSound; qt: LongInt; voicepack: PVoicepack): LongInt; -{*remember: LongInt = 32bit; integer = 16bit; byte = 8bit*} -function openal_init (filename: PChar; hardware: boolean; memsize: LongInt) : boolean; cdecl; external OpenALBridge; -function openal_close : boolean; cdecl; external OpenALBridge; -function openal_loadfile (const filename: PChar) : LongInt; cdecl; external OpenALBridge; -function openal_toggleloop (index: LongInt) : boolean; cdecl; external OpenALBridge; -function openal_setvolume (index: LongInt; percentage: byte) : boolean; cdecl; external OpenALBridge; -function openal_setglobalvolume (percentage: byte) : boolean; cdecl; external OpenALBridge; -function openal_fadeout (index: LongInt; quantity: SmallInt) : boolean; cdecl; external OpenALBridge; -function openal_fadein (index: LongInt; quantity: SmallInt) : boolean; cdecl; external OpenALBridge; -function openal_fade (index: LongInt; quantity: SmallInt; - direction: boolean) : boolean; cdecl; external OpenALBridge; -function openal_playsound (index: LongInt) : boolean; cdecl; external OpenALBridge; -function openal_pausesound (index: LongInt) : boolean; cdecl; external OpenALBridge; -function openal_stopsound (index: LongInt) : boolean; cdecl; external OpenALBridge; +function oalb_init(const app: PChar; const usehardware: Byte): Byte; cdecl; external OpenALBridge; +procedure oalb_close; cdecl; external OpenALBridge; +function oalb_loadfile(const filename: PChar): LongInt; cdecl; external OpenALBridge; +procedure oalb_playsound(const idx: LongInt; const loop: Byte); cdecl; external OpenALBridge; +procedure oalb_stopsound(const idx: LongInt); cdecl; external OpenALBridge; +procedure oalb_pausesound(const idx: LongInt); cdecl; external OpenALBridge; +procedure oalb_continuesound(const idx: LongInt); cdecl; external OpenALBridge; +procedure oalb_setvolume(const idx: LongInt; const percentage: Byte); cdecl; external OpenALBridge; +procedure oalb_setglobalvolume(const percentage: Byte); cdecl; external OpenALBridge; + var MusicFN: shortstring = ''; @@ -101,14 +97,13 @@ end; procedure InitSound; -const numSounds = 80; begin if not isSoundEnabled then exit; {*sound works in ipodtouch only if LAND_WIDTH = 1024; LAND_HEIGHT = 512; or if ogg are loaded in stream or if sound is loaded by demand*} WriteToConsole('Init OpenAL sound...'); -isSoundEnabled:= openal_init(str2pchar(ParamStr(0)), isSoundHardware, numSounds); +isSoundEnabled:= oalb_init('hwengine', Byte(isSoundHardware)) = 1; if isSoundEnabled then WriteLnToConsole(msgOK) else WriteLnToConsole(msgFailed); @@ -118,8 +113,8 @@ procedure ReleaseSound; begin -if isMusicEnabled then openal_fadeout(Mus, 30); -openal_close(); +//if isMusicEnabled then openal_fadeout(Mus, 30); +oalb_close(); end; procedure SoundLoad; @@ -136,7 +131,7 @@ begin s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName; WriteToConsole(msgLoading + s + ' '); - defVoicepack^.chunks[i]:= openal_loadfile (Str2PChar(s)); + defVoicepack^.chunks[i]:= oalb_loadfile(Str2PChar(s)); TryDo(defVoicepack^.chunks[i] >= 0, msgFailed, true); WriteLnToConsole(msgOK); end; @@ -148,7 +143,7 @@ begin s:= Pathz[Soundz[i].Path] + '/' + voicepacks[t].name + '/' + Soundz[i].FileName; WriteToConsole(msgLoading + s + ' '); - voicepacks[t].chunks[i]:= openal_loadfile (Str2PChar(s)); + voicepacks[t].chunks[i]:= oalb_loadfile(Str2PChar(s)); if voicepacks[t].chunks[i] < 0 then WriteLnToConsole(msgFailed) else @@ -159,38 +154,27 @@ function soundFadeOut(snd: TSound; qt: LongInt; voicepack: PVoicepack): LongInt; begin if not isSoundEnabled then exit(0); -if (voicepack <> nil) and (voicepack^.chunks[snd] >= 0) then openal_fadeout(defVoicepack^.chunks[snd], qt) -else if (defVoicepack^.chunks[snd] >= 0) then openal_fadeout(defVoicepack^.chunks[snd], qt); +//if (voicepack <> nil) and (voicepack^.chunks[snd] >= 0) then openal_fadeout(defVoicepack^.chunks[snd], qt) +//else if (defVoicepack^.chunks[snd] >= 0) then openal_fadeout(defVoicepack^.chunks[snd], qt); end; procedure PlaySound(snd: TSound; infinite: boolean; voicepack: PVoicepack); begin if (not isSoundEnabled) or fastUntilLag then exit; -if (voicepack <> nil) then -begin +if voicepack = nil then voicepack:= defVoicepack; + if voicepack^.chunks[snd] >= 0 then begin - if infinite then openal_toggleloop(voicepack^.chunks[snd]); - openal_playsound(voicepack^.chunks[snd]); + oalb_playsound(voicepack^.chunks[snd], Byte(infinite)); lastChan[snd]:=voicepack^.chunks[snd]; end -end -else -begin -if (defVoicepack^.chunks[snd] >= 0) then - begin - if infinite then openal_toggleloop(defVoicepack^.chunks[snd]); - openal_playsound(defVoicepack^.chunks[snd]); - lastChan[snd]:=defVoicepack^.chunks[snd]; - end -end end; procedure StopSound(snd: TSound); begin if isSoundEnabled then - openal_stopsound(lastChan[snd]) + oalb_stopsound(lastChan[snd]) end; procedure PlayMusic; @@ -203,12 +187,13 @@ s:= PathPrefix + '/Music/' + MusicFN; WriteToConsole(msgLoading + s + ' '); -Mus:= openal_loadfile(Str2PChar(s)); +Mus:= oalb_loadfile(Str2PChar(s)); TryDo(Mus >= 0, msgFailed, false); WriteLnToConsole(msgOK); -openal_fadein(Mus, 20); -openal_toggleloop(Mus); +//openal_fadein(Mus, 20); +//openal_toggleloop(Mus); +oalb_playsound(Mus, 1); end; function ChangeVolume(voldelta: LongInt): LongInt; @@ -219,21 +204,21 @@ if Volume < 0 then Volume:= 0; if Volume > 100 then Volume:= 100; -openal_setglobalvolume(Volume); -if isMusicEnabled then openal_setvolume(Mus, Volume shr 1); +oalb_setglobalvolume(Volume); +if isMusicEnabled then oalb_setvolume(Mus, Volume shr 1); ChangeVolume:= Volume; end; procedure PauseMusic; begin if (MusicFN = '') or (not isMusicEnabled) then exit; -openal_pausesound(Mus); +oalb_stopsound(Mus) end; procedure ResumeMusic; begin if (MusicFN = '') or (not isMusicEnabled) then exit; -openal_playsound(Mus); +oalb_playsound(Mus, 0) end; diff -r 228757f6c54d -r fececcbc2189 openalbridge/CMakeLists.txt --- a/openalbridge/CMakeLists.txt Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/CMakeLists.txt Wed Oct 14 22:12:02 2009 +0000 @@ -18,7 +18,7 @@ if(WIN32) #workaround for visualstudio (wants headers in the source list) set(openal_src - openalbridge.h loaders.h wrappers.h globals.h oggvorbis.h errlib.h ${openal_src} + openalbridge.h loaders.h wrappers.h common.h oggvorbis.h errlib.h ${openal_src} ) #deps for the shared library link_libraries(${VORBISFILE_LIBRARY}) diff -r 228757f6c54d -r fececcbc2189 openalbridge/errlib.c --- a/openalbridge/errlib.c Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/errlib.c Wed Oct 14 22:12:02 2009 +0000 @@ -9,7 +9,7 @@ */ #include "errlib.h" - +#include #define MAXLINE 4095 diff -r 228757f6c54d -r fececcbc2189 openalbridge/errlib.h --- a/openalbridge/errlib.h Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/errlib.h Wed Oct 14 22:12:02 2009 +0000 @@ -11,7 +11,7 @@ #ifndef _ERRLIB_H #define _ERRLIB_H -#include "globals.h" +#include "common.h" #ifdef __CPLUSPLUS extern "C" { diff -r 228757f6c54d -r fececcbc2189 openalbridge/globals.h --- a/openalbridge/globals.h Wed Oct 14 16:30:41 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,154 +0,0 @@ -/* - * OpenAL Bridge - a simple portable library for OpenAL interface - * Copyright (c) 2009 Vittorio Giovara - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; version 2 of the License - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef _OALB_GLOBALS_H -#define _OALB_GLOBALS_H - -#include -#include -#include -#include -#include -#include - -#ifndef _WIN32 -#include -#include -#else -#include -#define syslog(x,y) fprintf(stderr,y) -#define LOG_INFO 6 -#define LOG_ERR 3 -#endif - -#include "al.h" -#include "errlib.h" - - -/*control debug verbosity*/ -#ifdef TRACE -#ifndef DEBUG -#define DEBUG -#endif -#endif - -/** 1.0 02/03/10 - Defines cross-platform sleep, usleep, etc. [Wu Yongwei] **/ -#ifndef _SLEEP_H -#define _SLEEP_H -#ifdef _WIN32 -# if defined(_NEED_SLEEP_ONLY) && (defined(_MSC_VER) || defined(__MINGW32__)) -# include -# define sleep(t) _sleep((t) * 1000) -# else -# define WIN32_LEAN_AND_MEAN -# include -# define sleep(t) Sleep((t) * 1000) -# endif -# ifndef _NEED_SLEEP_ONLY -# define msleep(t) Sleep(t) -# define usleep(t) Sleep((t) / 1000) -# endif -#else -# include -# ifndef _NEED_SLEEP_ONLY -# define msleep(t) usleep((t) * 1000) -# endif -#endif -#endif /* _SLEEP_H */ - - -/* check compiler requirements */ /*FIXME*/ -#if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__) -#warning __BIG_ENDIAN__ or __LITTLE_ENDIAN__ not found, going to set __LITTLE_ENDIAN__ as default -#define __LITTLE_ENDIAN__ 1 -//#error Do not know the endianess of this architecture -#endif - -/* use byteswap macros from the host system, hopefully optimized ones ;-) - * or define our own version, simple, stupid, straight-forward... */ -#ifdef HAVE_BYTESWAP_H -#include -#else -#define bswap_16(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) -#define bswap_32(x) ((((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | \ -(((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) ) -#endif /* HAVE_BYTESWAP_H */ - -/* swap numbers accordingly to architecture automatically */ -#ifdef __LITTLE_ENDIAN__ -#define ENDIAN_LITTLE_32(x) x -#define ENDIAN_BIG_32(x) bswap_32(x) -#define ENDIAN_LITTLE_16(x) x -#define ENDIAN_BIG_16(x) bswap_16(x) -#elif __BIG_ENDIAN__ -#define ENDIAN_LITTLE_32(x) bswap_32(x) -#define ENDIAN_BIG_32(x) x -#define ENDIAN_LITTLE_16(x) bswap_16(x) -#define ENDIAN_BIG_16(x) x -#endif - - -#ifdef __CPLUSPLUS -extern "C" { -#endif - - /*data type for WAV header*/ -#pragma pack(1) - typedef struct _WAV_header_t { - uint32_t ChunkID; - uint32_t ChunkSize; - uint32_t Format; - uint32_t Subchunk1ID; - uint32_t Subchunk1Size; - uint16_t AudioFormat; - uint16_t NumChannels; - uint32_t SampleRate; - uint32_t ByteRate; - uint16_t BlockAlign; - uint16_t BitsPerSample; - uint32_t Subchunk2ID; - uint32_t Subchunk2Size; - } WAV_header_t; -#pragma pack() - - /*data type for passing data between threads*/ -#pragma pack(1) - typedef struct _fade_t { - uint32_t index; - uint16_t quantity; - } fade_t; -#pragma pack() - - - /*file format defines*/ -#define OGG_FILE_FORMAT 0x4F676753 -#define WAV_FILE_FORMAT 0x52494646 -#define WAV_HEADER_SUBCHUNK2ID 0x64617461 - - - /*other defines*/ -#define FADE_IN 0 -#define FADE_OUT 1 - - char *prog; - -#ifdef __CPLUSPLUS -} -#endif - -#endif /*_OALB_GLOBALS_H*/ diff -r 228757f6c54d -r fececcbc2189 openalbridge/loaders.c --- a/openalbridge/loaders.c Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/loaders.c Wed Oct 14 22:12:02 2009 +0000 @@ -1,6 +1,7 @@ /* * OpenAL Bridge - a simple portable library for OpenAL interface - * Copyright (c) 2009 Vittorio Giovara + * Copyright (c) 2009 Vittorio Giovara , + * Mario Liebisch * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -19,229 +20,223 @@ #include "loaders.h" -#ifdef __CPLUSPLUS -extern "C" { -#endif +int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) { + WAV_header_t WAVHeader; + FILE *wavfile; + int32_t t; + uint32_t n = 0; + uint8_t sub0, sub1, sub2, sub3; + + wavfile = Fopen(filename, "rb"); + + fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile); /*RIFF*/ + fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile); + fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile); /*WAVE*/ + +#ifdef DEBUG + fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID)); + fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize)); + fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format)); +#endif + + fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile); /*fmt */ + fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile); + fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile); + fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile); + fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile); + fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile); + fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile); + fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile); - int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) { - WAV_header_t WAVHeader; - FILE *wavfile; - int32_t t; - uint32_t n = 0; - uint8_t sub0, sub1, sub2, sub3; - - wavfile = Fopen(filename, "rb"); - - fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile); /*RIFF*/ - fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile); - fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile); /*WAVE*/ - #ifdef DEBUG - fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID)); - fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize)); - fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format)); + fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID)); + fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size)); + fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat)); + fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels)); + fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate)); + fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate)); + fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign)); + fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample)); #endif - - fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile); /*fmt */ - fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile); - fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile); - fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile); - fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile); - fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile); - fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile); - fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile); + + /*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */ + do { + t = fread(&sub0, sizeof(uint8_t), 1, wavfile); + if(sub0 == 0x64) { + t = fread(&sub1, sizeof(uint8_t), 1, wavfile); + if(sub1 == 0x61) { + t = fread(&sub2, sizeof(uint8_t), 1, wavfile); + if(sub2 == 0x74) { + t = fread(&sub3, sizeof(uint8_t), 1, wavfile); + if(sub3 == 0x61) { + WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID; + break; + } + } + } + } + if (t <= 0) { + /*eof*/ + errno = EILSEQ; + err_ret("(%s) ERROR - wrong WAV header", prog); + return AL_FALSE; + } + } while (1); + + fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile); + #ifdef DEBUG - fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID)); - fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size)); - fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat)); - fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels)); - fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate)); - fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate)); - fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign)); - fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample)); + fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID)); + fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); #endif - - /*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */ - do { - t = fread(&sub0, sizeof(uint8_t), 1, wavfile); - if(sub0 == 0x64) { - t = fread(&sub1, sizeof(uint8_t), 1, wavfile); - if(sub1 == 0x61) { - t = fread(&sub2, sizeof(uint8_t), 1, wavfile); - if(sub2 == 0x74) { - t = fread(&sub3, sizeof(uint8_t), 1, wavfile); - if(sub3 == 0x61) { - WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID; - break; - } - } - } - } - - if (t <= 0) { - /*eof*/ + + *data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); + + /*read the actual sound data*/ + do { + n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile); + } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); + + fclose(wavfile); + +#ifdef DEBUG + err_msg("(%s) INFO - WAV data loaded", prog); +#endif + + /*set parameters for OpenAL*/ + /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/ + if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) { + if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8) + *format = AL_FORMAT_MONO8; + else { + if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16) + *format = AL_FORMAT_MONO16; + else { errno = EILSEQ; - err_ret("(%s) ERROR - wrong WAV header", prog); + err_ret("(%s) ERROR - wrong WAV header [bitsample value]", prog); return AL_FALSE; } - } while (1); - - fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile); - -#ifdef DEBUG - fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID)); - fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); -#endif - - *data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); - - /*read the actual sound data*/ - do { - n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile); - } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); - - fclose(wavfile); - -#ifdef DEBUG - err_msg("(%s) INFO - WAV data loaded", prog); -#endif - - /*set parameters for OpenAL*/ - /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/ - if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) { + } + } else { + if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) { if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8) - *format = AL_FORMAT_MONO8; + *format = AL_FORMAT_STEREO8; else { if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16) - *format = AL_FORMAT_MONO16; + *format = AL_FORMAT_STEREO16; else { errno = EILSEQ; err_ret("(%s) ERROR - wrong WAV header [bitsample value]", prog); return AL_FALSE; - } - } + } + } } else { - if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) { - if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8) - *format = AL_FORMAT_STEREO8; - else { - if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16) - *format = AL_FORMAT_STEREO16; - else { - errno = EILSEQ; - err_ret("(%s) ERROR - wrong WAV header [bitsample value]", prog); - return AL_FALSE; - } - } - } else { - errno = EILSEQ; - err_ret("(%s) ERROR - wrong WAV header [format value]", prog); - return AL_FALSE; - } + errno = EILSEQ; + err_ret("(%s) ERROR - wrong WAV header [format value]", prog); + return AL_FALSE; } - - *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size); - *freq = ENDIAN_LITTLE_32(WAVHeader.SampleRate); - return AL_TRUE; } + *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size); + *freq = ENDIAN_LITTLE_32(WAVHeader.SampleRate); + return AL_TRUE; +} + + +int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) { + /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */ - int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) { - /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */ - - /*stream handle*/ - OggVorbis_File oggStream; - /*some formatting data*/ - vorbis_info *vorbisInfo; - /*length of the decoded data*/ - int64_t pcm_length; - /*other vars*/ - int section, result, size, endianness; + /*stream handle*/ + OggVorbis_File oggStream; + /*some formatting data*/ + vorbis_info *vorbisInfo; + /*length of the decoded data*/ + int64_t pcm_length; + /*other vars*/ + int section, result, size, endianness; +#ifdef DEBUG + int i; + /*other less useful data*/ + vorbis_comment *vorbisComment; +#endif + + result = ov_fopen((char*) filename, &oggStream); + if (result < 0) { + errno = EINVAL; + err_ret("(%s) ERROR - ov_fopen() failed with %X", prog, result); + ov_clear(&oggStream); + return AL_FALSE; + } + + /*load OGG header and determine the decoded data size*/ + vorbisInfo = ov_info(&oggStream, -1); + pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels; + #ifdef DEBUG - int i; - /*other less useful data*/ - vorbis_comment *vorbisComment; + vorbisComment = ov_comment(&oggStream, -1); + fprintf(stderr, "Version: %d\n", vorbisInfo->version); + fprintf(stderr, "Channels: %d\n", vorbisInfo->channels); + fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate); + fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper); + fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal); + fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower); + fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window); + fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor); + fprintf(stderr, "PCM data size: %lld\n", pcm_length); + fprintf(stderr, "# comment: %d\n", vorbisComment->comments); + for (i = 0; i < vorbisComment->comments; i++) + fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]); #endif - - result = ov_fopen((char*) filename, &oggStream); - if (result < 0) { - errno = EINVAL; - err_ret("(%s) ERROR - ov_fopen() failed with %X", prog, result); + + /*allocates enough room for the decoded data*/ + *data = (char*) Malloc (sizeof(char) * pcm_length); + + /*there *should* not be ogg at 8 bits*/ + if (vorbisInfo->channels == 1) + *format = AL_FORMAT_MONO16; + else { + if (vorbisInfo->channels == 2) + *format = AL_FORMAT_STEREO16; + else { + errno = EILSEQ; + err_ret("(%s) ERROR - wrong OGG header [channel %d]", prog, vorbisInfo->channels); ov_clear(&oggStream); return AL_FALSE; } - - /*load OGG header and determine the decoded data size*/ - vorbisInfo = ov_info(&oggStream, -1); - pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels; - -#ifdef DEBUG - vorbisComment = ov_comment(&oggStream, -1); - fprintf(stderr, "Version: %d\n", vorbisInfo->version); - fprintf(stderr, "Channels: %d\n", vorbisInfo->channels); - fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate); - fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper); - fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal); - fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower); - fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window); - fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor); - fprintf(stderr, "PCM data size: %lld\n", pcm_length); - fprintf(stderr, "# comment: %d\n", vorbisComment->comments); - for (i = 0; i < vorbisComment->comments; i++) - fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]); + } + + size = 0; +#ifdef __LITTLE_ENDIAN__ + endianness = 0; +#elif __BIG_ENDIAN__ + endianness = 1; #endif - - /*allocates enough room for the decoded data*/ - *data = (char*) Malloc (sizeof(char) * pcm_length); - - /*there *should* not be ogg at 8 bits*/ - if (vorbisInfo->channels == 1) - *format = AL_FORMAT_MONO16; - else { - if (vorbisInfo->channels == 2) - *format = AL_FORMAT_STEREO16; - else { + while (size < pcm_length) { + /*ov_read decodes the ogg stream and storse the pcm in data*/ + result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, §ion); + if (result > 0) { + size += result; + } else { + if (result == 0) + break; + else { errno = EILSEQ; - err_ret("(%s) ERROR - wrong OGG header [channel %d]", prog, vorbisInfo->channels); + err_ret("(%s) ERROR - End of file from OGG stream", prog); ov_clear(&oggStream); return AL_FALSE; } } - - size = 0; -#ifdef __LITTLE_ENDIAN__ - endianness = 0; -#elif __BIG_ENDIAN__ - endianness = 1; -#endif - while (size < pcm_length) { - /*ov_read decodes the ogg stream and storse the pcm in data*/ - result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, §ion); - if (result > 0) { - size += result; - } else { - if (result == 0) - break; - else { - errno = EILSEQ; - err_ret("(%s) ERROR - End of file from OGG stream", prog); - ov_clear(&oggStream); - return AL_FALSE; - } - } - } - - /*set the last fields*/ - *bitsize = size; - *freq = vorbisInfo->rate; - - /*cleaning time*/ - ov_clear(&oggStream); - - return AL_TRUE; } -#ifdef __CPLUSPLUS + /*set the last fields*/ + *bitsize = size; + *freq = vorbisInfo->rate; + + /*cleaning time*/ + ov_clear(&oggStream); + + return AL_TRUE; } -#endif + + diff -r 228757f6c54d -r fececcbc2189 openalbridge/loaders.h --- a/openalbridge/loaders.h Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/loaders.h Wed Oct 14 22:12:02 2009 +0000 @@ -1,6 +1,7 @@ /* * OpenAL Bridge - a simple portable library for OpenAL interface - * Copyright (c) 2009 Vittorio Giovara + * Copyright (c) 2009 Vittorio Giovara , + * Mario Liebisch * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -19,10 +20,10 @@ #ifndef _OALB_LOADERS_H #define _OALB_LOADERS_H -#include "globals.h" +#include "common.h" #include "wrappers.h" #include "oggvorbis.h" - +extern char *prog; #ifdef __CPLUSPLUS extern "C" { diff -r 228757f6c54d -r fececcbc2189 openalbridge/openalbridge.c --- a/openalbridge/openalbridge.c Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/openalbridge.c Wed Oct 14 22:12:02 2009 +0000 @@ -1,6 +1,7 @@ /* * OpenAL Bridge - a simple portable library for OpenAL interface - * Copyright (c) 2009 Vittorio Giovara + * Copyright (c) 2009 Vittorio Giovara , + * Mario Liebisch * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -19,480 +20,594 @@ #include "openalbridge.h" -#ifdef __CPLUSPLUS -extern "C" { -#endif - - /*Sources are points emitting sound*/ - ALuint *Sources; - /*Buffers hold sound data*/ - ALuint *Buffers; - /*index for Sources and Buffers*/ - ALuint globalindex, globalsize, increment; - - ALboolean openalReady = AL_FALSE; +char *prog; + +/*Buffers hold sound data*/ +ALuint *Buffers; +/*index for Sources and Buffers*/ +ALuint globalindex, globalsize, increment; + +//null vector +const ALfloat NV[] = {0.0f, 0.0f, 0.0f}; +//listener orientation +const ALfloat LO[] = {0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}; + +SSound_t aSounds[MAX_SOUNDS]; +int iNumSounds = 0; +char oalbReady = 0; + +ALCcontext *context; +ALCdevice *device; +ALuint sources[MAX_SOURCES]; + + + + + +/** + * const char oalb_init (const char* programname, const char usehardware) + * + * ARGUMENTS + * programname [const char*]: name of the program invoking OpenAL + * usehardware [const char] : flag to enable audio hardware acceleration + * DESCRIPTION + * + * RETURN + * 1 success + * 2 error + */ + +char oalb_init (const char* programname, const char usehardware) { + prog = (char *) programname; - ALboolean openal_close (void) { - /*Stop all sounds, deallocate all memory and close OpenAL */ - ALCcontext *context; - ALCdevice *device; - - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - alSourceStopv (globalsize, Sources); - alDeleteSources (globalsize, Sources); - alDeleteBuffers (globalsize, Buffers); - - free(Sources); - free(Buffers); - - context = alcGetCurrentContext(); - device = alcGetContextsDevice(context); - - alcMakeContextCurrent(NULL); - alcDestroyContext(context); - alcCloseDevice(device); - - openalReady = AL_FALSE; - - return AL_TRUE; + if (oalbReady == AL_TRUE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL already initialized", prog); + return AL_FALSE; } - ALboolean openal_ready(void) { - return openalReady; +#ifdef _WIN32 + /* Hardware acceleration is broken on some windows card*/ + if (usehardware != 0) + device = alcOpenDevice(NULL); + else + { + device = alcOpenDevice("Generic Software"); + if(!device) + { + err_msg("(%s) WARN - Failed to open software device", prog); + device = alcOpenDevice(NULL); + } + } +#else + /*always hardware for UNIX systems*/ + device = alcOpenDevice(NULL); +#endif + + if (device == NULL) { + errno = ENODEV; + err_ret("(%s) WARN - Failed to open sound device", prog); + return AL_FALSE; } - ALboolean openal_init(char* programname, ALboolean usehardware, uint32_t memorysize) { - /*Initialize an OpenAL contex and allocate memory space for data and buffers*/ - ALCcontext *context; - ALCdevice *device; - const ALCchar *default_device; - - prog = programname; - - /*Position of the listener*/ - ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; - /*Velocity of the listener*/ - ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; - /*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/ - ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; - - if (openalReady == AL_TRUE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL already initialized", prog); - return AL_FALSE; - } - - if (usehardware == AL_TRUE) - device = alcOpenDevice(NULL); - else - device = alcOpenDevice("Generic Software"); - - if (device == NULL) { - errno = ENODEV; - err_ret("(%s) WARN - Failed to open sound device", prog); - return AL_FALSE; - } - err_msg("(%s) INFO - Output device: %s", prog, alcGetString(device, ALC_DEVICE_SPECIFIER)); - - context = alcCreateContext(device, NULL); - alcMakeContextCurrent(context); - alcProcessContext(context); - - if (AlGetError("(%s) WARN - Failed to create a new contex") != AL_TRUE) - return AL_FALSE; - - /*allocate memory space for buffers and sources*/ - if (memorysize == 0) - globalsize = 50; - else - globalsize = memorysize; - increment = globalsize; - - Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); - Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); - - /*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/ - alListenerf (AL_GAIN, 1.0f ); - alListenerfv(AL_POSITION, ListenerPos); - alListenerfv(AL_VELOCITY, ListenerVel); - alListenerfv(AL_ORIENTATION, ListenerOri); - - if (AlGetError("(%s) WARN - Failed to set Listener properties") != AL_TRUE) - return AL_FALSE; - - openalReady = AL_TRUE; - - alGetError(); /* clear any AL errors beforehand */ - return AL_TRUE; - } + err_msg("(%s) INFO - Using OpenAL device: %s", prog, alcGetString(device, ALC_DEVICE_SPECIFIER)); + context = alcCreateContext(device, NULL); + alcMakeContextCurrent(context); - ALboolean helper_realloc (void) { - /*expands allocated memory when loading more sound files than expected*/ - int oldsize = globalsize; - globalsize += increment; - -#ifdef DEBUG - err_msg("(%s) INFO - Realloc in process from %d to %d\n", prog, oldsize, globalsize); -#endif - - Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); - Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); - - return AL_TRUE; - } + if (AlGetError("(%s) WARN - Failed to create a new contex") != AL_TRUE) + return AL_FALSE; - ALint openal_loadfile (const char *filename){ - /*Open a file, load into memory and allocate the Source buffer for playing*/ - ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/ - ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/ - ALenum format; - ALsizei bitsize, freq; - char *data; - uint32_t fileformat; - ALenum error; - FILE *fp; - - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - /*when the buffers are all used, we can expand memory to accept new files*/ - if (globalindex == globalsize) - helper_realloc(); - - /*detect the file format, as written in the first 4 bytes of the header*/ - fp = Fopen (filename, "rb"); - - if (fp == NULL) - return -1; - - error = fread (&fileformat, sizeof(uint32_t), 1, fp); - fclose (fp); - - if (error < 0) { - errno = EIO; - err_ret("(%s) ERROR - File %s is too short", prog, filename); - return -2; - } - - /*prepare the buffer to receive data*/ - alGenBuffers(1, &Buffers[globalindex]); - - if (AlGetError("(%s) ERROR - Failed to allocate memory for buffers") != AL_TRUE) - return -3; - - /*prepare the source to emit sound*/ - alGenSources(1, &Sources[globalindex]); - - if (AlGetError("(%s) ERROR - Failed to allocate memory for sources") != AL_TRUE) - return -4; - - - switch (ENDIAN_BIG_32(fileformat)) { - case OGG_FILE_FORMAT: - error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); - break; - case WAV_FILE_FORMAT: - error = load_wavpcm (filename, &format, &data, &bitsize, &freq); - break; - default: - errno = EINVAL; - err_ret ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(fileformat)); - return -5; - break; - } - - - /*copy pcm data in one buffer*/ - alBufferData(Buffers[globalindex], format, data, bitsize, freq); - free(data); /*deallocate data to save memory*/ - - if (AlGetError("(%s) ERROR - Failed to write data to buffers") != AL_TRUE) - return -6; - - /*set source properties that it will use when it's in playback*/ - alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); - alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); - alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); - alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); - alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); - alSourcei (Sources[globalindex], AL_LOOPING, 0 ); - - if (AlGetError("(%s) ERROR - Failed to set Source properties") != AL_TRUE) - return -7; - - alGetError(); /* clear any AL errors beforehand */ - - /*returns the index of the source you just loaded, increments it and exits*/ - return globalindex++; + /*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/ + alListenerf (AL_GAIN, 1.0f); + alListenerfv(AL_POSITION, NV); + alListenerfv(AL_VELOCITY, NV); + alListenerfv(AL_ORIENTATION, LO); + + alcProcessContext(context); + + if (AlGetError("(%s) WARN - Failed to set Listener properties") != AL_TRUE) + return AL_FALSE; + + alGenSources(MAX_SOURCES, sources); + + oalbReady = AL_TRUE; + + alGetError(); /* clear any AL errors beforehand */ + return AL_TRUE; +} + + +/** + * void oalb_close(void) + * + * ARGUMENTS + * - + * DESCRIPTION + * + * RETURN + * - + */ + +void oalb_close(void) { + /*Stop all sounds, deallocate all memory and close OpenAL */ + int i; + + if (oalbReady == 0) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return; + } + + for(i = 0; i < iNumSounds; i++) { + SSound_stop(&aSounds[i]); + SSound_close(&aSounds[i]); + } + + alSourceStopv (MAX_SOURCES, sources); + alDeleteSources (MAX_SOURCES, sources); + + alcMakeContextCurrent(NULL); + alcDestroyContext(context); + alcCloseDevice(device); + + oalbReady = 0; + + return; +} + +char oalb_ready(void) { + return oalbReady; +} + +/** + * const int32_t oalb_loadfile (const char *filename) + * + * ARGUMENTS + * - + * DESCRIPTION + * + * RETURN + * - + */ + +int32_t oalb_loadfile (const char *filename) { + int i; + + if (oalbReady == 0) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return -1; + } + + if(iNumSounds == MAX_SOUNDS) { + err_msg("(%s) WARN - Maximum number of sound samples reached", prog); + return -3; } - ALboolean openal_toggleloop (uint32_t index){ - /*Set or unset looping mode*/ - ALint loop; - - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - if (index >= globalsize) { - errno = EINVAL; - err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); - return AL_FALSE; - } - - alGetSourcei (Sources[index], AL_LOOPING, &loop); - alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); - if (AlGetError("(%s) ERROR - Failed to get or set loop property") != AL_TRUE) - return AL_FALSE; - - alGetError(); /* clear any AL errors beforehand */ - - return AL_TRUE; + for(i = 0; i < iNumSounds; i++) + if(strcmp(aSounds[i].Filename, filename) == 0) + return i; + + if(SSound_load(&aSounds[iNumSounds], filename)) + return iNumSounds++; + else + return -2; + +} + +/* + ALboolean openal_toggleloop (uint32_t index){ + /*Set or unset looping mode + ALint loop; + + if (oalbReady == AL_FALSE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return AL_FALSE; + } + + if (index >= globalsize) { + errno = EINVAL; + err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); + return AL_FALSE; + } + + alGetSourcei (Sources[index], AL_LOOPING, &loop); + alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); + if (AlGetError("(%s) ERROR - Failed to get or set loop property") != AL_TRUE) + return AL_FALSE; + + alGetError(); /* clear any AL errors beforehand + + return AL_TRUE; + } + */ + +void oalb_setvolume (const uint32_t iIndex, const char cPercentage) { + if (oalbReady == 0) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return; + } + + /*Set volume for sound number index*/ + if(iIndex < 0 || iIndex >= iNumSounds) { + errno = EINVAL; + err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); + return; + } + + if(cPercentage > 100) + SSound_volume(&aSounds[iIndex], 1.0f); + else + SSound_volume(&aSounds[iIndex], cPercentage / 100.0f); + + if (AlGetError2("(%s) ERROR - Failed to set volume for sound %d\n", iIndex) != AL_TRUE) + return; + + alGetError(); /* clear any AL errors beforehand */ + + return; +} + + +void oalb_setglobalvolume (const char cPercentage) { + if (oalbReady == 0) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return; } + /*Set volume for all sounds*/ + if(cPercentage > 100) + alListenerf (AL_GAIN, 1.0f); + else + alListenerf (AL_GAIN, cPercentage / 100.0f); - ALboolean openal_setvolume (uint32_t index, uint8_t percentage) { - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - /*Set volume for sound number index*/ - if (index >= globalsize) { - errno = EINVAL; - err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); - return AL_FALSE; - } - - if (percentage > 100) - percentage = 100; - alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f); - if (AlGetError2("(%s) ERROR - Failed to set volume for sound %d\n", index) != AL_TRUE) - return AL_FALSE; - - alGetError(); /* clear any AL errors beforehand */ - - return AL_TRUE; + if (AlGetError("(%s) ERROR - Failed to set global volume") != AL_TRUE) + return; + + alGetError(); /* clear any AL errors beforehand */ + + return; +} + +/* + ALboolean openal_togglemute () { + /*Mute or unmute sound + ALfloat mute; + + if (oalbReady == AL_FALSE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return AL_FALSE; + } + + alGetListenerf (AL_GAIN, &mute); + if (mute > 0) + mute = 0; + else + mute = 1.0; + alListenerf (AL_GAIN, mute); + if (AlGetError("(%s) ERROR - Failed to set mute property") != AL_TRUE) + return AL_FALSE; + + alGetError(); /* clear any AL errors beforehand + + return AL_TRUE; + } + + + ALboolean openal_fade (uint32_t index, uint16_t quantity, ALboolean direction) { + /*Fade in or out by calling a helper thread + #ifndef _WIN32 + pthread_t thread; + #else + HANDLE Thread; + DWORD threadID; + #endif + fade_t *fade; + + if (oalbReady == AL_FALSE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return AL_FALSE; + } + + fade = (fade_t*) Malloc(sizeof(fade_t)); + fade->index = index; + fade->quantity = quantity; + + if (index >= globalsize) { + errno = EINVAL; + err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); + return AL_FALSE; + } + + switch (direction) { + case FADE_IN: + #ifndef _WIN32 + pthread_create(&thread, NULL, helper_fadein, (void*) fade); + #else + Thread = _beginthread(&helper_fadein, 0, (void*) fade); + #endif + break; + case FADE_OUT: + #ifndef _WIN32 + pthread_create(&thread, NULL, helper_fadeout, (void*) fade); + #else + Thread = _beginthread(&helper_fadeout, 0, (void*) fade); + #endif + break; + default: + errno = EINVAL; + err_ret("(%s) ERROR - Unknown direction for fading", prog, index, globalindex); + free(fade); + return AL_FALSE; + break; + } + + #ifndef _WIN32 + pthread_detach(thread); + #endif + + alGetError(); /* clear any AL errors beforehand + + return AL_TRUE; + } + + + ALboolean openal_fadeout (uint32_t index, uint16_t quantity) { + /*wrapper for fadeout + return openal_fade(index, quantity, FADE_OUT); + } + + + ALboolean openal_fadein (uint32_t index, uint16_t quantity) { + /*wrapper for fadein + return openal_fade(index, quantity, FADE_IN); + } + + + ALboolean openal_setposition (uint32_t index, float x, float y, float z) { + if (oalbReady == AL_FALSE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return AL_FALSE; + } + + if (index >= globalsize) { + errno = EINVAL; + err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); + return AL_FALSE; + } + + alSource3f(Sources[index], AL_POSITION, x, y, z); + if (AlGetError2("(%s) ERROR - Failed to set position for sound %d)", index) != AL_TRUE) + return AL_FALSE; + + return AL_TRUE; + } + + */ + +void oalb_playsound (const uint32_t iIndex, const char bLoop) { + if (oalbReady == AL_FALSE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return; } - - ALboolean openal_setglobalvolume (uint8_t percentage) { - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - /*Set volume for all sounds*/ - if (percentage > 100) - percentage = 100; - alListenerf (AL_GAIN, (float) percentage/100.0f); - if (AlGetError("(%s) ERROR - Failed to set global volume") != AL_TRUE) - return AL_FALSE; - - alGetError(); /* clear any AL errors beforehand */ - - return AL_TRUE; + /*Play sound number index*/ + if(iIndex < 0 || iIndex >= iNumSounds) { + errno = EINVAL; + err_ret("(%s) ERROR - Index (%d) out of bounds", prog, iIndex); + return; } - - - ALboolean openal_togglemute () { - /*Mute or unmute sound*/ - ALfloat mute; - - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - alGetListenerf (AL_GAIN, &mute); - if (mute > 0) - mute = 0; - else - mute = 1.0; - alListenerf (AL_GAIN, mute); - if (AlGetError("(%s) ERROR - Failed to set mute property") != AL_TRUE) - return AL_FALSE; - - alGetError(); /* clear any AL errors beforehand */ - - return AL_TRUE; - } + SSound_play(&aSounds[iIndex], bLoop); - ALboolean openal_fade (uint32_t index, uint16_t quantity, ALboolean direction) { - /*Fade in or out by calling a helper thread*/ -#ifndef _WIN32 - pthread_t thread; -#else - HANDLE Thread; - DWORD threadID; -#endif - fade_t *fade; - - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - fade = (fade_t*) Malloc(sizeof(fade_t)); - fade->index = index; - fade->quantity = quantity; - - if (index >= globalsize) { - errno = EINVAL; - err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); - return AL_FALSE; - } - - switch (direction) { - case FADE_IN: -#ifndef _WIN32 - pthread_create(&thread, NULL, helper_fadein, (void*) fade); -#else - Thread = _beginthread(&helper_fadein, 0, (void*) fade); -#endif - break; - case FADE_OUT: -#ifndef _WIN32 - pthread_create(&thread, NULL, helper_fadeout, (void*) fade); -#else - Thread = _beginthread(&helper_fadeout, 0, (void*) fade); -#endif - break; - default: - errno = EINVAL; - err_ret("(%s) ERROR - Unknown direction for fading", prog, index, globalindex); - free(fade); - return AL_FALSE; - break; - } - -#ifndef _WIN32 - pthread_detach(thread); -#endif - - alGetError(); /* clear any AL errors beforehand */ - - return AL_TRUE; + + alGetError(); /* clear any AL errors beforehand */ + + return; +} + + +void oalb_pausesound (const uint32_t iIndex) { + if (oalbReady == AL_FALSE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return; + } + + /*Pause sound number index*/ + if(iIndex < 0 || iIndex >= iNumSounds) { + errno = EINVAL; + err_ret("(%s) ERROR - Index (%d) out of bounds", prog, iIndex); + return; + } + SSound_pause(&aSounds[iIndex]); + + if (AlGetError2("(%s) ERROR - Failed to pause sound %d)", iIndex) != AL_TRUE) + return; + + return; +} + + +void oalb_stopsound (const uint32_t iIndex) { + if (oalbReady == AL_FALSE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); + return; } + /*Stop sound number index*/ + if(iIndex < 0 || iIndex >= iNumSounds) { + errno = EINVAL; + err_ret("(%s) ERROR - Index (%d) out of bounds", prog, iIndex); + return; + } + SSound_stop(&aSounds[iIndex]); - ALboolean openal_fadeout (uint32_t index, uint16_t quantity) { - /*wrapper for fadeout*/ - return openal_fade(index, quantity, FADE_OUT); + if (AlGetError2("(%s) ERROR - Failed to stop sound %d)", iIndex) != AL_TRUE) + return; + + alGetError(); /* clear any AL errors beforehand */ + + return; +} + + +/*SSOUND STUFF HERE*/ + +char SSound_load (SSound_t* pSound, const char* cFilename) { + uint32_t magic; + ALenum format; + ALsizei bitsize, freq; + char *data; + FILE* fp; + + snprintf(pSound->Filename, 256, "%s", cFilename); + pSound->source = -1; + alGenBuffers(1, &pSound->Buffer); + + if(alGetError() != AL_NO_ERROR) { + fprintf(stderr, "CSound: Couldn't create buffer.\n"); + return 0; } + fp = fopen(pSound->Filename, "rb"); - ALboolean openal_fadein (uint32_t index, uint16_t quantity) { - /*wrapper for fadein*/ - return openal_fade(index, quantity, FADE_IN); + if(!fp) { + fprintf(stderr, "CSound: Couldn't open file for reading.\n"); + return 0; + } + + if(fread(&magic, sizeof(uint32_t), 1, fp) < 1) + { + fclose(fp); + fprintf(stderr, "CSound: Couldn't read file header.\n"); + return 0; + } + fclose(fp); + + switch (ENDIAN_BIG_32(magic)) { + case OGG_FILE_FORMAT: + load_oggvorbis (pSound->Filename, &format, &data, &bitsize, &freq); + break; + case WAV_FILE_FORMAT: + load_wavpcm (pSound->Filename, &format, &data, &bitsize, &freq); + break; + default: + errno = EINVAL; + err_ret ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(magic)); + return 0; + break; } - - ALboolean openal_setposition (uint32_t index, float x, float y, float z) { - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - if (index >= globalsize) { - errno = EINVAL; - err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); - return AL_FALSE; - } - - alSource3f(Sources[index], AL_POSITION, x, y, z); - if (AlGetError2("(%s) ERROR - Failed to set position for sound %d)", index) != AL_TRUE) - return AL_FALSE; - - return AL_TRUE; + alBufferData(pSound->Buffer, format, data, bitsize, freq); + if(alGetError() != AL_NO_ERROR) + { + fprintf(stderr, "CSound: Couldn't write buffer data.\n"); + return 0; } - + free(data); - ALboolean openal_playsound (uint32_t index){ - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - /*Play sound number index*/ - if (index >= globalsize) { - errno = EINVAL; - err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); - return AL_FALSE; - } - alSourcePlay(Sources[index]); - if (AlGetError2("(%s) ERROR - Failed to play sound %d)", index) != AL_TRUE) - return AL_FALSE; - - alGetError(); /* clear any AL errors beforehand */ - - return AL_TRUE; - } - + return 1; +} + +void SSound_close(SSound_t* pSound) +{ + SSound_stop(pSound); + alDeleteBuffers(1, &pSound->Buffer); +} + +void SSound_play(SSound_t* pSound, const char bLoop) { + int i; - ALboolean openal_pausesound(uint32_t index){ - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; + if(pSound->source == -1) // need a new source + { + int i; + for(i = 0; i < MAX_SOURCES; i++) + { + ALint state; + alGetSourcei(sources[i], AL_SOURCE_STATE, &state); + if(state != AL_PLAYING && state != AL_PAUSED) + { +#ifdef DEBUG + printf("using source %d (state 0x%x) for buffer.\n", i, state); +#endif + alSourceStop(sources[pSound->source]); + alGetError(); + break; + } } - - /*Pause sound number index*/ - if (index >= globalsize) { - errno = EINVAL; - err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); - return AL_FALSE; + if(i == MAX_SOURCES) // no available source found; skip + { +#ifdef DEBUG + printf("no source to play buffer %d!\n", i); +#endif + return; } - alSourcePause(Sources[index]); - if (AlGetError2("(%s) ERROR - Failed to pause sound %d)", index) != AL_TRUE) - return AL_FALSE; - - return AL_TRUE; + pSound->source = i; + } + else // reuse already playing source + { + alSourceStop(sources[pSound->source]); } - + alSourcei (sources[pSound->source], AL_BUFFER, pSound->Buffer); + alSourcef (sources[pSound->source], AL_PITCH, 1.0f); + alSourcef (sources[pSound->source], AL_GAIN, 1.0f); + alSourcefv(sources[pSound->source], AL_POSITION, NV ); + alSourcefv(sources[pSound->source], AL_VELOCITY, NV ); + alSourcei (sources[pSound->source], AL_LOOPING, bLoop ); + alSourcePlay(sources[pSound->source]); - ALboolean openal_stopsound(uint32_t index){ - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return AL_FALSE; - } - - /*Stop sound number index*/ - if (index >= globalsize) { - errno = EINVAL; - err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex); - return AL_FALSE; - } - alSourceStop(Sources[index]); - if (AlGetError2("(%s) ERROR - Failed to stop sound %d)", index) != AL_TRUE) - return AL_FALSE; - - alGetError(); /* clear any AL errors beforehand */ - - return AL_TRUE; + if((i = alGetError()) != AL_NO_ERROR) + { + fprintf(stderr, "CSound: SourcePlay error 0x%4x in source %d\n", i, pSound->source); } - -#ifdef __CPLUSPLUS +#ifdef DEBUG + fprintf(stderr, "play %s%s [%d]\n", pSound->Filename, bLoop ? " forever" : " once", pSound->source); +#endif +} + +void SSound_pause(const SSound_t* pSound) { + if(pSound->source == -1) // not playing + return; + alSourcePause(sources[pSound->source]); +#ifdef DEBUG + fprintf(stderr, "pause %s\n", pSound->Filename); +#endif } + +void SSound_continue(const SSound_t* pSound) { + if(pSound->source == -1) // not playing + return; + alSourcePlay(sources[pSound->source]); +#ifdef DEBUG + fprintf(stderr, "pause %s\n", pSound->Filename); #endif +} + +void SSound_stop(SSound_t* pSound) { + if(pSound->source == -1) // not playing + return; + alSourceStop(sources[pSound->source]); + pSound->source = -1; +#ifdef DEBUG + fprintf(stderr, "stop %s\n", pSound->Filename); +#endif +} + +void SSound_volume(const SSound_t* pSound, const float fPercentage) { + if(pSound->source == -1) // not playing + return; + alSourcef(sources[pSound->source], AL_GAIN, fPercentage); +} + diff -r 228757f6c54d -r fececcbc2189 openalbridge/openalbridge.h --- a/openalbridge/openalbridge.h Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/openalbridge.h Wed Oct 14 22:12:02 2009 +0000 @@ -19,33 +19,44 @@ #ifndef _OALB_INTERFACE_H #define _OALB_INTERFACE_H -#include "globals.h" +#include "common.h" #include "wrappers.h" +#include "loaders.h" #include "alc.h" -#include "loaders.h" -#ifdef __CPLUSPLUS +#ifdef __cplusplus extern "C" { #endif - ALboolean openal_init (char* programname, ALboolean usehardware, unsigned int memorysize); - ALboolean openal_close (void); - ALboolean openal_ready (void); - ALint openal_loadfile (const char *filename); + char oalb_init (const char* programname, const char usehardware); + void oalb_close (void); + char oalb_ready (void); + int32_t oalb_loadfile (const char* cFilename); + void oalb_playsound (const uint32_t iIndex, const char bLoop); + void oalb_pausesound (const uint32_t iIndex); + void oalb_stopsound (const uint32_t iIndex); + void oalb_setvolume (const uint32_t iIndex, const char cPercentage); + void oalb_setglobalvolume (const char cPercentage); + + /* ALboolean openal_toggleloop (unsigned int index); ALboolean openal_setposition (unsigned int index, float x, float y, float z); - ALboolean openal_setvolume (unsigned int index, unsigned char percentage); - ALboolean openal_setglobalvolume (unsigned char percentage); ALboolean openal_togglemute (void); ALboolean openal_fadeout (unsigned int index, unsigned short int quantity); ALboolean openal_fadein (unsigned int index, unsigned short int quantity); ALboolean openal_fade (unsigned int index, unsigned short int quantity, ALboolean direction); - ALboolean openal_playsound (unsigned int index); ALboolean openal_pausesound (unsigned int index); - ALboolean openal_stopsound (unsigned int index); + */ -#ifdef __CPLUSPLUS + char SSound_load (SSound_t* pSound, const char* cFilename); + void SSound_close (SSound_t* pSound); + void SSound_play (SSound_t* pSound, const char bLoop); + void SSound_pause (const SSound_t* pSound); + void SSound_continue (const SSound_t* pSound); + void SSound_stop (SSound_t* pSound); + void SSound_volume (const SSound_t* pSound, const float fPercentage); +#ifdef __cplusplus } #endif diff -r 228757f6c54d -r fececcbc2189 openalbridge/wrappers.c --- a/openalbridge/wrappers.c Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/wrappers.c Wed Oct 14 22:12:02 2009 +0000 @@ -18,6 +18,7 @@ #include "wrappers.h" +extern char *prog; #ifdef __CPLUSPLUS extern "C" { @@ -78,7 +79,7 @@ return AL_TRUE; } - void *helper_fadein(void *tmp) { + /* void *helper_fadein(void *tmp) { ALfloat gain; ALfloat target_gain; fade_t *fade; @@ -94,7 +95,7 @@ err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity); #endif - /*save the volume desired after the fade*/ + /*save the volume desired after the fade alGetSourcef(Sources[index], AL_GAIN, &target_gain); if (target_gain > 1.0f || target_gain <= 0.0f) target_gain = 1.0f; @@ -147,7 +148,7 @@ AlGetError("(%s) WARN - Failed to set fade-out volume level"); - /*stop that sound and reset its volume*/ + /*stop that sound and reset its volume alSourceStop (Sources[index]); alSourcef (Sources[index], AL_GAIN, old_gain); @@ -159,7 +160,7 @@ return 0; } - + */ #ifdef __CPLUSPLUS } #endif diff -r 228757f6c54d -r fececcbc2189 openalbridge/wrappers.h --- a/openalbridge/wrappers.h Wed Oct 14 16:30:41 2009 +0000 +++ b/openalbridge/wrappers.h Wed Oct 14 22:12:02 2009 +0000 @@ -19,7 +19,7 @@ #ifndef _OALB_WRAPPERS_H #define _OALB_WRAPPERS_H -#include "globals.h" +#include "common.h" #ifdef __CPLUSPLUS