From 9d312559cfc84e5d0d79fda924d9047f0672a7b6 Mon Sep 17 00:00:00 2001 From: Sonicadvance1 Date: Sun, 27 Jul 2008 14:07:52 +0000 Subject: [PATCH] Linux: Add libao to DSL_NULL plugin so I get audio in linux git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@89 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Src/PCHW/AOSoundStream.cpp | 126 ++++++++++++++++++ .../Plugin_DSP_NULL/Src/PCHW/AOSoundStream.h | 35 +++++ Source/Plugins/Plugin_DSP_NULL/Src/SConscript | 4 +- Source/Plugins/Plugin_DSP_NULL/Src/main.cpp | 6 +- 4 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 Source/Plugins/Plugin_DSP_NULL/Src/PCHW/AOSoundStream.cpp create mode 100644 Source/Plugins/Plugin_DSP_NULL/Src/PCHW/AOSoundStream.h diff --git a/Source/Plugins/Plugin_DSP_NULL/Src/PCHW/AOSoundStream.cpp b/Source/Plugins/Plugin_DSP_NULL/Src/PCHW/AOSoundStream.cpp new file mode 100644 index 0000000000..5aea45c992 --- /dev/null +++ b/Source/Plugins/Plugin_DSP_NULL/Src/PCHW/AOSoundStream.cpp @@ -0,0 +1,126 @@ +#include +#include +#include "AOSoundStream.h" + +namespace AOSound +{ + pthread_t thread; + StreamCallback callback; + + char *buffer; + int buf_size; + + ao_device *device; + ao_sample_format format; + int default_driver; + + int bufferSize; + int totalRenderedBytes; + int sampleRate; + volatile int threadData; + int currentPos; + int lastPos; + short realtimeBuffer[1024 * 1024]; + int AOSound_GetSampleRate() + { + return sampleRate; + } + bool WriteDataToBuffer(int dwOffset,char* soundData, int dwSoundBytes) + { + //void* ptr1, * ptr2; + //DWORD numBytes1, numBytes2; + // Obtain memory address of write block. This will be in two parts if the block wraps around. + //HRESULT hr = dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0); + + // If the buffer was lost, restore and retry lock. + /*if (DSERR_BUFFERLOST == hr) + { + dsBuffer->Restore(); + hr = dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0); + } + + if (SUCCEEDED(hr)) + { + memcpy(ptr1, soundData, numBytes1); + + if (ptr2 != 0) + { + memcpy(ptr2, soundData + numBytes1, numBytes2); + } + + // Release the data back to DirectSound. + dsBuffer->Unlock(ptr1, numBytes1, ptr2, numBytes2); + return(true); + } + + return(false);*/ + ao_play(device, soundData, dwSoundBytes); + return true; + + } + + void* soundThread(void*) + { + currentPos = 0; + lastPos = 0; + + // Prefill buffer? + //writeDataToBuffer(0,realtimeBuffer,bufferSize); + // dsBuffer->Lock(0, bufferSize, (void **)&p1, &num1, (void **)&p2, &num2, 0); + //dsBuffer->Play(0, 0, DSBPLAY_LOOPING); + + while (!threadData) + { + // No blocking inside the csection + //dsBuffer->GetCurrentPosition((DWORD*)¤tPos, 0); + int numBytesToRender = 256; + + if (numBytesToRender >= 256) + { + (*callback)(realtimeBuffer, numBytesToRender >> 2, 16, 44100, 2); + + WriteDataToBuffer(0, (char*)realtimeBuffer, numBytesToRender); + //currentPos = ModBufferSize(lastPos + numBytesToRender); + //totalRenderedBytes += numBytesToRender; + + //lastPos = currentPos; + } + + //WaitForSingleObject(soundSyncEvent, MAXWAIT); + } + + //dsBuffer->Stop(); + return(0); //hurra! + } + bool AOSound_StartSound(int _sampleRate, StreamCallback _callback) + { + callback = _callback; + threadData = 0; + sampleRate = _sampleRate; + + //no security attributes, automatic resetting, init state nonset, untitled + //soundSyncEvent = CreateEvent(0, false, false, 0); + ao_initialize(); + default_driver = ao_default_driver_id(); + format.bits = 16; + format.channels = 2; + format.rate = sampleRate; + format.byte_format = AO_FMT_LITTLE; + + //vi vill ha access till DSOUND så... + device = ao_open_live(default_driver, &format, NULL /* no options */); + if (device == NULL) { + fprintf(stderr, "Error opening device.\n"); + return 1; + } + buf_size = format.bits/8 * format.channels * format.rate; + buffer = (char*)calloc(buf_size, sizeof(char)); + pthread_create(&thread, NULL, soundThread, (void *)NULL); + return(true); + } + void AOSound_StopSound() + { + ao_close(device); + ao_shutdown(); + } +} diff --git a/Source/Plugins/Plugin_DSP_NULL/Src/PCHW/AOSoundStream.h b/Source/Plugins/Plugin_DSP_NULL/Src/PCHW/AOSoundStream.h new file mode 100644 index 0000000000..dfef628e0c --- /dev/null +++ b/Source/Plugins/Plugin_DSP_NULL/Src/PCHW/AOSoundStream.h @@ -0,0 +1,35 @@ +// Copyright (C) 2003-2008 Dolphin Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0. + +// 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 General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official SVN repository and contact information can be found at +// http://code.google.com/p/dolphin-emu/ + +#ifndef __AOSOUNDSTREAM_H__ +#define __AOSOUNDSTREAM_H__ + +namespace AOSound +{ +typedef void (*StreamCallback)(short* buffer, int numSamples, int bits, int rate, int channels); + +bool AOSound_StartSound(int sampleRate, StreamCallback _callback); +void AOSound_UpdateSound(); +void AOSound_StopSound(); + +float AOSound_GetTimer(); +int AOSound_GetCurSample(); +int AOSound_GetSampleRate(); +} + + +#endif //__AOSOUNDSTREAM_H__ diff --git a/Source/Plugins/Plugin_DSP_NULL/Src/SConscript b/Source/Plugins/Plugin_DSP_NULL/Src/SConscript index 4d7c66883b..aaf10d3c2b 100644 --- a/Source/Plugins/Plugin_DSP_NULL/Src/SConscript +++ b/Source/Plugins/Plugin_DSP_NULL/Src/SConscript @@ -5,6 +5,7 @@ files = ["DSPHandler.cpp", "main.cpp", "Config.cpp", "Globals.cpp", + "PCHW/AOSoundStream.cpp", "PCHW/Mixer.cpp", "UCodes/UCode_AX.cpp", "UCodes/UCode_CARD.cpp", @@ -14,4 +15,5 @@ files = ["DSPHandler.cpp", "UCodes/UCodes.cpp", "UCodes/UCode_Zelda.cpp", ] -env.SharedLibrary("../../../../Binary/linux/Plugins/dsphle.so", files, LIBS = ["common"]) +dspenv=env.Copy(LINKFLAGS = "`pkg-config --libs ao` ") +dspenv.SharedLibrary("../../../../Binary/linux/Plugins/dsphle.so", files, LIBS = ["common"]) diff --git a/Source/Plugins/Plugin_DSP_NULL/Src/main.cpp b/Source/Plugins/Plugin_DSP_NULL/Src/main.cpp index 1d495b9f77..a4b17a322c 100644 --- a/Source/Plugins/Plugin_DSP_NULL/Src/main.cpp +++ b/Source/Plugins/Plugin_DSP_NULL/Src/main.cpp @@ -24,7 +24,7 @@ #include "AboutDlg.h" #include "ConfigDlg.h" #else -// TODO +#include "PCHW/AOSoundStream.h" #endif #include "PCHW/Mixer.h" @@ -110,6 +110,8 @@ void DSP_Initialize(DSPInitialize _dspInitialize) #ifdef _WIN32 DSound::DSound_StartSound((HWND)g_dspInitialize.hWnd, g_Config.m_SampleRate, Mixer); +#else + AOSound::AOSound_StartSound(g_Config.m_SampleRate, Mixer); #endif } @@ -119,6 +121,8 @@ void DSP_Shutdown() // delete the UCodes #ifdef _WIN32 DSound::DSound_StopSound(); +#else + AOSound::AOSound_StopSound(); #endif CDSPHandler::Destroy(); }