From ab7037523314a27117748f3ed2808bc8df20d3cd Mon Sep 17 00:00:00 2001 From: Denis Semenov Date: Thu, 5 Aug 2021 13:55:01 +0300 Subject: [PATCH] remake player --- .../SynthesizeKit/Streamer/STCAudioPlayer.h | 5 +- .../SynthesizeKit/Streamer/STCAudioPlayer.m | 128 +++++++++++------- 2 files changed, 79 insertions(+), 54 deletions(-) diff --git a/SpeechproSpeechKit/SpeechproSpeechKit/SynthesizeKit/Streamer/STCAudioPlayer.h b/SpeechproSpeechKit/SpeechproSpeechKit/SynthesizeKit/Streamer/STCAudioPlayer.h index d35eadb..f74a7bd 100644 --- a/SpeechproSpeechKit/SpeechproSpeechKit/SynthesizeKit/Streamer/STCAudioPlayer.h +++ b/SpeechproSpeechKit/SpeechproSpeechKit/SynthesizeKit/Streamer/STCAudioPlayer.h @@ -29,11 +29,10 @@ AudioQueueRef queue; AudioQueueBufferRef mBuffers[kNumberBuffers]; AudioStreamBasicDescription playFormat; - int index; - int buffersNum; NSLock *sysnLock; + BOOL audioQueueUsed[kNumberBuffers]; + BOOL audioQueueIsStarted; @public - Boolean isRunning; Boolean isInitialized; int pip_fd[2]; UInt32 numPacketsToRead; diff --git a/SpeechproSpeechKit/SpeechproSpeechKit/SynthesizeKit/Streamer/STCAudioPlayer.m b/SpeechproSpeechKit/SpeechproSpeechKit/SynthesizeKit/Streamer/STCAudioPlayer.m index 2f1d869..4ff9fa3 100644 --- a/SpeechproSpeechKit/SpeechproSpeechKit/SynthesizeKit/Streamer/STCAudioPlayer.m +++ b/SpeechproSpeechKit/SpeechproSpeechKit/SynthesizeKit/Streamer/STCAudioPlayer.m @@ -12,41 +12,37 @@ @implementation STCAudioPlayer @synthesize queue; @synthesize bufferByteSize; -void AQBufferCallback(void * inUserData , - AudioQueueRef inAQ, - AudioQueueBufferRef inBuffer) { +void AQBufferCallback(void * inUserData, + AudioQueueRef inAQ, + AudioQueueBufferRef inBuffer) { STCAudioPlayer *THIS = (__bridge STCAudioPlayer *)(inUserData); - - + if (THIS->isInitialized == NO) { + return; + } ssize_t res = 0; - - if(THIS->isRunning) { - - if(THIS->bufferByteSize <= 0){ - THIS->buffersNum -= 1; - if(THIS->buffersNum == 0) { - [THIS playEnded]; - } - return; - } - + if(THIS->bufferByteSize > 0) { res = read(THIS->pip_fd[0], inBuffer->mAudioData, MIN(THIS->bufferByteSize, 4000)); + } + if(res > 0 ){ THIS->bufferByteSize -= res; inBuffer->mPacketDescriptionCount = res/2; - inBuffer->mAudioDataByteSize = res; - - if(res > 0 ){ - AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL); + AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL); + } else { + int bufferIndex = [THIS getIndexForBuffer: inBuffer]; + if(bufferIndex >= 0) { + THIS->audioQueueUsed[bufferIndex] = NO; + if(![THIS bufferHasUsed]) { + [THIS playEnded]; + } } } } --(void) playEnded { +-(void)playEnded { if( self.delegate != nil) { [[self delegate] playEnded:self]; } - } -(id)init { @@ -57,7 +53,6 @@ -(id)initWithSampleRate:(int)sampleRate { self = [super init]; if(self) { sysnLock = [[NSLock alloc]init]; - buffersNum = 0; memset(&playFormat, 0, sizeof(playFormat)); playFormat.mFormatID = kAudioFormatLinearPCM; playFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked ; @@ -66,30 +61,28 @@ -(id)initWithSampleRate:(int)sampleRate { playFormat.mBytesPerPacket = playFormat.mBytesPerFrame = (playFormat.mBitsPerChannel / 8) * playFormat.mChannelsPerFrame; playFormat.mFramesPerPacket = 1; playFormat.mSampleRate = sampleRate; - isRunning = false; isInitialized = false; + audioQueueIsStarted = false; } return self; } -(void)start { + [sysnLock lock]; if (isInitialized) { + [sysnLock unlock]; return; } - [sysnLock lock]; + isInitialized = true; bufferByteSize = 0; AudioQueueNewOutput(&playFormat, AQBufferCallback, (__bridge void *)(self), nil, nil, 0, &queue); for (int i=0; imAudioData, pcmData, dataSize); - mBuffers[index]->mAudioDataByteSize = dataSize; - mBuffers[index]->mPacketDescriptionCount = dataSize/2; - AudioQueueEnqueueBuffer(queue, mBuffers[index], 0, NULL); - NSLog(@"fill audio queue buffer[%d]",index); - if(index == kNumberBuffers - 1) { - isRunning = true; - index = 0; + int bufferIndex = [self getNotUsedBufferIndex]; + if (bufferIndex >= 0) { + AudioQueueBufferRef audioQueueBuffer = mBuffers[bufferIndex]; + memcpy(audioQueueBuffer->mAudioData, pcmData, dataSize); + audioQueueBuffer->mAudioDataByteSize = dataSize; + audioQueueBuffer->mPacketDescriptionCount = dataSize/2; + AudioQueueEnqueueBuffer(queue, audioQueueBuffer, 0, NULL); + audioQueueUsed[bufferIndex] = YES; + if (!audioQueueIsStarted && ![self bufferHasNotUsed]) { + audioQueueIsStarted = true; AudioQueueStart(queue, NULL); - } else { - index++; } } else { bufferByteSize += dataSize; if(write(pip_fd[1], pcmData, dataSize) < 0){ - NSLog(@"write to the pipe failed!"); + NSLog(@"STCAudioPlayer: write to the pipe failed!"); } } [sysnLock unlock]; } + +-(BOOL)bufferHasUsed { + for (int i = 0; i < kNumberBuffers; i++) { + if (YES == audioQueueUsed[i]) { + return YES; + } + } + return NO; +} + +-(BOOL)bufferHasNotUsed { + for (int i = 0; i < kNumberBuffers; i++) { + if (NO == audioQueueUsed[i]) { + return YES; + } + } + return NO; +} + +-(int)getNotUsedBufferIndex { + for (int i = 0; i < kNumberBuffers; i++) { + if (NO == audioQueueUsed[i]) { + return i; + } + } + return -1; +} + +-(int)getIndexForBuffer: (AudioQueueBufferRef) audioQueueBuffer{ + for (int i = 0; i < kNumberBuffers; i++) { + if (audioQueueBuffer == mBuffers[i]) { + return i; + } + } + return -1; +} + @end