Author: derek Date: Sun Nov 12 15:56:43 2006 New Revision: 170 Modified: sofaswitch/trunk/sound/audio.cxx sofaswitch/trunk/sound/audio.h Log: Give up on PQueueChannel, and go for a simple simple mutex protected buffer. Modified: sofaswitch/trunk/sound/audio.cxx ============================================================================== --- sofaswitch/trunk/sound/audio.cxx (original) +++ sofaswitch/trunk/sound/audio.cxx Sun Nov 12 15:56:43 2006 @@ -70,11 +70,10 @@ TestAudioWrite::TestAudioWrite(PUDPSocket &_socket, PSoundChannel &_sound) : TestAudio(_socket, _sound) { - - audioQueue = new PQueueChannel(); - audioQueue->Open(1000); - audioQueue->SetReadTimeout(40); - audioQueue->SetWriteTimeout(40); + memset(silenceBuffer, 0, oneChunk); + memset(buffer, 0, buildSize); + readIndex = 0; + writeIndex = 0; PTRACE(3, "Writer\tInitiate thread for writing " ); Resume(); @@ -82,8 +81,6 @@ TestAudioWrite::~TestAudioWrite() { - audioQueue->Close(); - delete(audioQueue); } void TestAudioWrite::Main() @@ -114,8 +111,17 @@ continue; } PINDEX readSize = socket.GetLastReadCount(); - data.SetSize(readSize); - audioQueue->Write(data.GetPointer(), data.GetSize()); + PWaitAndSignal m(accessMutex); + if ((writeIndex + readSize) < buildSize) { + memcpy (buffer + writeIndex, data.GetPointer(), readSize); + writeIndex += readSize; + } else { + PINDEX firstHalf = buildSize - writeIndex; + PINDEX secondHalf = readSize - firstHalf; + memcpy (buffer + writeIndex, data.GetPointer(), firstHalf); + memcpy(buffer, data.GetPointer() + firstHalf, secondHalf); + writeIndex = secondHalf; + } PTRACE(6, "postWRITE of " << data.GetSize() << " bytes to sound device"); countPackets++; } @@ -133,28 +139,30 @@ void TestAudioWrite::QueueToSpeakerMain(PThread &, INT) { -#define BUFFER_SIZE 1000 - silenceData.SetSize(480); - memset(silenceData.GetPointer(), 0, 480); - incomingData.SetSize(BUFFER_SIZE); - memset(incomingData.GetPointer(), 0, BUFFER_SIZE); - - PINDEX storedCount = 0; while(keepGoing) { - if (audioQueue->Read(incomingData.GetPointer() + storedCount, 240)) { - PINDEX count = audioQueue->GetLastReadCount(); - PTRACE(6, "Read " << count << "bytes from PQueueChannel. to have a total of " << (storedCount + count)); - if ((storedCount + count) > 480) { - sound.Write(incomingData.GetPointer(), 480); - PTRACE(6, "Play 480 bytes to the speaker "); - memcpy(incomingData.GetPointer(), incomingData.GetPointer() + 480, (storedCount + count) - 480); - storedCount = (storedCount + count) - 480; + { + PWaitAndSignal m(accessMutex); + PINDEX available; + if (writeIndex < readIndex) + available = (writeIndex + buildSize) - readIndex; + else + available = writeIndex - readIndex; + if (available > (oneChunk + (oneChunk >> 1))) { + if ((readIndex + oneChunk) < buildSize) { + memcpy (soundBuffer, buffer + readIndex, oneChunk); + readIndex += oneChunk; + } else { + PINDEX firstHalf = buildSize - readIndex; + PINDEX secondHalf = oneChunk - firstHalf; + memcpy(soundBuffer, buffer + readIndex, firstHalf); + memcpy(soundBuffer + firstHalf, buffer, secondHalf); + readIndex = secondHalf; + } } else - storedCount += count; - PTRACE(6, "Have " << storedCount << " byte waiting to be played"); - } else { - sound.Write(silenceData.GetPointer(), 480); + memcpy(soundBuffer, silenceBuffer, oneChunk); } + sound.Write(soundBuffer, oneChunk); + PTRACE(6, "Play " << oneChunk << " bytes to the speaker "); } PTRACE(4, "End of queue to speaker main thread "); Modified: sofaswitch/trunk/sound/audio.h ============================================================================== --- sofaswitch/trunk/sound/audio.h (original) +++ sofaswitch/trunk/sound/audio.h Sun Nov 12 15:56:43 2006 @@ -1,17 +1,10 @@ /* * audio.h * - * PWLib application header file for sound card + jabber + * PWLib application header file for sound card in a SofaSwitch environment. * * * - * - * - * - * - * - * - * */ #ifndef _AUDIO_H @@ -115,17 +108,35 @@ PDECLARE_NOTIFIER(PThread, TestAudioWrite, QueueToSpeakerMain); #endif + /**The number of bytes in the buffer we use for building sound into + sections of the right size */ + enum { + /** 5000 is 0.3125 of a second of audio data, which is heaps. Such a + large number saves us from wrapping round the end too often.*/ + buildSize= 5000, + /**30 ms of audio */ + oneChunk = 480 + }; + + /**The mutex which protects access to the buffer and buffer variables */ + PMutex accessMutex; - /**The pwlib structure which takes the input sound samples (which may come - in blocks of 160, 240, 320, 480, or other bytes) and glues them - together to give one nice stream of 480 byte blocks. */ - PQueueChannel *audioQueue; + /**The current read index, which goes 0 .. (buildSize -1) */ + PINDEX readIndex; - /**The buffer of data we use to collect incoming data into */ - PBYTEArray incomingData; + /**The current write index, which goes 0 .. (buildSize -1) */ + PINDEX writeIndex; - /**The data we send to the speaker when it is silence time */ - PBYTEArray silenceData; + /**The region of memor for accumulating incoming and outgoing data */ + char buffer[buildSize]; + + /**The buffer used to play silence from if there is not enough audio. This + is not mutex protected. */ + char silenceBuffer[oneChunk]; + + /**The buffer used to contain audio which is given to the speaker. This is + not mutex protected. */ + char soundBuffer[oneChunk]; };
participants (1)
-
SofaSWITCH SVN