From 32d433019570b5644cb4a4f39b0f1d411f522fa8 Mon Sep 17 00:00:00 2001 From: Pavel Lukandiy Date: Fri, 14 Apr 2017 15:00:20 +0300 Subject: [PATCH 1/2] Playback speed --- StreamingKit/StreamingKit/STKAudioPlayer.h | 3 ++ StreamingKit/StreamingKit/STKAudioPlayer.m | 36 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.h b/StreamingKit/StreamingKit/STKAudioPlayer.h index 0ce6d53..e864059 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.h +++ b/StreamingKit/StreamingKit/STKAudioPlayer.h @@ -239,6 +239,9 @@ typedef void(^STKFrameFilter)(UInt32 channelsPerFrame, UInt32 bytesPerFrame, UIn /// Disposes the STKAudioPlayer and frees up all resources before returning -(void) dispose; +// +-(void)setPlaybackSpeed:(double)speed; + /// The QueueItemId of the currently playing item -(NSObject*) currentlyPlayingQueueItemId; diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index c97ec36..61508f7 100755 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -174,6 +174,7 @@ static UInt32 maxFramesPerSlice = 4096; static AudioComponentDescription mixerDescription; static AudioComponentDescription nbandUnitDescription; static AudioComponentDescription outputUnitDescription; +static AudioComponentDescription playbackSpeedUnitDescription; static AudioComponentDescription convertUnitDescription; static AudioStreamBasicDescription canonicalAudioStreamBasicDescription; static AudioStreamBasicDescription recordAudioStreamBasicDescription; @@ -201,6 +202,7 @@ static AudioStreamBasicDescription recordAudioStreamBasicDescription; AUNode eqNode; AUNode mixerNode; AUNode outputNode; + AUNode playbackSpeedNode; AUNode eqInputNode; AUNode eqOutputNode; @@ -210,6 +212,7 @@ static AudioStreamBasicDescription recordAudioStreamBasicDescription; AudioComponentInstance eqUnit; AudioComponentInstance mixerUnit; AudioComponentInstance outputUnit; + AudioComponentInstance playbackSpeedUnit; UInt32 eqBandCount; int32_t waitingForDataAfterSeekFrameCount; @@ -339,6 +342,14 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn .componentFlagsMask = 0, .componentManufacturer = kAudioUnitManufacturer_Apple }; + + playbackSpeedUnitDescription = (AudioComponentDescription) { + .componentType = kAudioUnitType_FormatConverter, + .componentSubType = kAudioUnitSubType_AUiPodTimeOther, + .componentFlags = 0, + .componentFlagsMask = 0, + .componentManufacturer = kAudioUnitManufacturer_Apple + }; mixerDescription = (AudioComponentDescription) { @@ -1866,6 +1877,10 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn [self stopThread]; } +- (void)setPlaybackSpeed:(double)speed { + AudioUnitSetParameter(playbackSpeedUnit, kNewTimePitchParam_Rate, kAudioUnitScope_Global, 0, speed, 0); +} + -(NSObject*) currentlyPlayingQueueItemId { OSSpinLockLock(¤tEntryReferencesLock); @@ -2126,6 +2141,20 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl CHECK_STATUS_AND_RETURN(AudioUnitSetProperty(outputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &canonicalAudioStreamBasicDescription, sizeof(canonicalAudioStreamBasicDescription))); } +- (void)createPlaybackSpeedUnit { + OSStatus status; + + CHECK_STATUS_AND_RETURN(AUGraphAddNode(audioGraph, &playbackSpeedUnitDescription, &playbackSpeedNode)); + CHECK_STATUS_AND_RETURN(AUGraphNodeInfo(audioGraph, playbackSpeedNode, &playbackSpeedUnitDescription, &playbackSpeedUnit)); + + CHECK_STATUS_AND_RETURN(AudioUnitSetProperty(playbackSpeedUnit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, kOutputBus, &maxFramesPerSlice, sizeof(maxFramesPerSlice))); +#if TARGET_OS_IPHONE + CHECK_STATUS_AND_RETURN(AudioUnitSetParameter(playbackSpeedUnit, kNewTimePitchParam_Rate, kAudioUnitScope_Global, 0, 1, 0)); +#endif + + CHECK_STATUS_AND_RETURN(AudioUnitSetProperty(playbackSpeedUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &canonicalAudioStreamBasicDescription, sizeof(canonicalAudioStreamBasicDescription))); +} + -(void) createMixerUnit { OSStatus status; @@ -2284,6 +2313,7 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl [self createEqUnit]; [self createMixerUnit]; + [self createPlaybackSpeedUnit]; [self createOutputUnit]; [self connectGraph]; @@ -2332,6 +2362,12 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl [nodes addObject:@(mixerNode)]; [units addObject:[NSValue valueWithPointer:mixerUnit]]; } + + if (playbackSpeedNode) + { + [nodes addObject:@(playbackSpeedNode)]; + [units addObject:[NSValue valueWithPointer:playbackSpeedUnit]]; + } if (outputNode) { From 4525951e5c4207e2b2296736c3165c29b27cc05b Mon Sep 17 00:00:00 2001 From: Pavel Lukandiy Date: Fri, 14 Apr 2017 18:22:01 +0300 Subject: [PATCH 2/2] Refactor --- StreamingKit/StreamingKit/STKAudioPlayer.h | 5 ++++- StreamingKit/StreamingKit/STKAudioPlayer.m | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.h b/StreamingKit/StreamingKit/STKAudioPlayer.h index e864059..792ba6e 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.h +++ b/StreamingKit/StreamingKit/STKAudioPlayer.h @@ -239,9 +239,12 @@ typedef void(^STKFrameFilter)(UInt32 channelsPerFrame, UInt32 bytesPerFrame, UIn /// Disposes the STKAudioPlayer and frees up all resources before returning -(void) dispose; -// +/// Sets playback speed -(void)setPlaybackSpeed:(double)speed; +/// Sets playback speed to 1.0 +-(void)setDefaultPlaybackSpeed; + /// The QueueItemId of the currently playing item -(NSObject*) currentlyPlayingQueueItemId; diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 61508f7..ae115dd 100755 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -343,7 +343,8 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn .componentManufacturer = kAudioUnitManufacturer_Apple }; - playbackSpeedUnitDescription = (AudioComponentDescription) { + playbackSpeedUnitDescription = (AudioComponentDescription) + { .componentType = kAudioUnitType_FormatConverter, .componentSubType = kAudioUnitSubType_AUiPodTimeOther, .componentFlags = 0, @@ -1877,10 +1878,16 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn [self stopThread]; } -- (void)setPlaybackSpeed:(double)speed { +-(void)setPlaybackSpeed:(double)speed +{ AudioUnitSetParameter(playbackSpeedUnit, kNewTimePitchParam_Rate, kAudioUnitScope_Global, 0, speed, 0); } +-(void)setDefaultPlaybackSpeed +{ + [self setPlaybackSpeed:1.f]; +} + -(NSObject*) currentlyPlayingQueueItemId { OSSpinLockLock(¤tEntryReferencesLock); @@ -2141,7 +2148,8 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl CHECK_STATUS_AND_RETURN(AudioUnitSetProperty(outputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &canonicalAudioStreamBasicDescription, sizeof(canonicalAudioStreamBasicDescription))); } -- (void)createPlaybackSpeedUnit { +-(void) createPlaybackSpeedUnit +{ OSStatus status; CHECK_STATUS_AND_RETURN(AUGraphAddNode(audioGraph, &playbackSpeedUnitDescription, &playbackSpeedNode));