Skip to content

Commit

Permalink
Rate change without glitches
Browse files Browse the repository at this point in the history
  • Loading branch information
HEnquist committed Feb 7, 2022
1 parent 54ecf85 commit d9f2b7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 13 additions & 5 deletions BlackHole/BlackHole.c
Expand Up @@ -224,6 +224,7 @@ static OSStatus BlackHole_Initialize(AudioServerPlugInDriverRef inDriver, AudioS
Float64 theHostClockFrequency = (Float64)theTimeBaseInfo.denom / (Float64)theTimeBaseInfo.numer;
theHostClockFrequency *= 1000000000.0;
gDevice_HostTicksPerFrame = theHostClockFrequency / gDevice_SampleRate;
gDevice_AdjustedTicksPerFrame = gDevice_HostTicksPerFrame - gDevice_HostTicksPerFrame/100.0 * 2.0*(gPitch_Adjust - 0.5);

// DebugMsg("BlackHole theTimeBaseInfo.numer: %u \t theTimeBaseInfo.denom: %u", theTimeBaseInfo.numer, theTimeBaseInfo.denom);

Expand Down Expand Up @@ -357,6 +358,7 @@ static OSStatus BlackHole_PerformDeviceConfigurationChange(AudioServerPlugInDriv
Float64 theHostClockFrequency = (Float64)theTimeBaseInfo.denom / (Float64)theTimeBaseInfo.numer;
theHostClockFrequency *= 1000000000.0;
gDevice_HostTicksPerFrame = theHostClockFrequency / gDevice_SampleRate;
gDevice_AdjustedTicksPerFrame = gDevice_HostTicksPerFrame - gDevice_HostTicksPerFrame/100.0 * 2.0*(gPitch_Adjust - 0.5);

// unlock the state mutex
pthread_mutex_unlock(&gPlugIn_StateMutex);
Expand Down Expand Up @@ -3695,6 +3697,7 @@ static OSStatus BlackHole_SetControlPropertyData(AudioServerPlugInDriverRef inDr
if(gPitch_Adjust != theNewPitch)
{
gPitch_Adjust = theNewPitch;
gDevice_AdjustedTicksPerFrame = gDevice_HostTicksPerFrame - gDevice_HostTicksPerFrame/100.0 * 2.0*(gPitch_Adjust - 0.5);
*outNumberPropertiesChanged = 1;
outChangedAddresses[0].mSelector = kAudioStereoPanControlPropertyValue;
outChangedAddresses[0].mScope = kAudioObjectPropertyScopeGlobal;
Expand All @@ -3703,9 +3706,10 @@ static OSStatus BlackHole_SetControlPropertyData(AudioServerPlugInDriverRef inDr
}
else
{
if(gVolume_Master_Value != theNewPitch)
if(gPitch_Adjust != theNewPitch)
{
gPitch_Adjust = theNewPitch;
gDevice_AdjustedTicksPerFrame = gDevice_HostTicksPerFrame - gDevice_HostTicksPerFrame/100.0 * 2.0*(gPitch_Adjust - 0.5);
*outNumberPropertiesChanged = 1;
outChangedAddresses[0].mSelector = kAudioStereoPanControlPropertyValue;
outChangedAddresses[0].mScope = kAudioObjectPropertyScopeGlobal;
Expand Down Expand Up @@ -3765,6 +3769,7 @@ static OSStatus BlackHole_StartIO(AudioServerPlugInDriverRef inDriver, AudioObje
gDevice_NumberTimeStamps = 0;
gDevice_AnchorSampleTime = 0;
gDevice_AnchorHostTime = mach_absolute_time();
gDevice_PreviousTicks = 0;

// allocate ring buffer
gRingBuffer = calloc(kRing_Buffer_Frame_Size * kNumber_Of_Channels, sizeof(Float32));
Expand Down Expand Up @@ -3841,7 +3846,8 @@ static OSStatus BlackHole_GetZeroTimeStamp(AudioServerPlugInDriverRef inDriver,
OSStatus theAnswer = 0;
UInt64 theCurrentHostTime;
Float64 theHostTicksPerRingBuffer;
Float64 theHostTickOffset;
Float64 theAdjustedTicksPerRingBuffer;
Float64 theNextTickOffset;
UInt64 theNextHostTime;

// check the arguments
Expand All @@ -3856,20 +3862,22 @@ static OSStatus BlackHole_GetZeroTimeStamp(AudioServerPlugInDriverRef inDriver,

// calculate the next host time
theHostTicksPerRingBuffer = gDevice_HostTicksPerFrame * ((Float64)kDevice_RingBufferSize);
theAdjustedTicksPerRingBuffer = gDevice_AdjustedTicksPerFrame * ((Float64)kDevice_RingBufferSize);

theHostTickOffset = ((Float64)(gDevice_NumberTimeStamps + 1)) * theHostTicksPerRingBuffer;
theNextTickOffset = gDevice_PreviousTicks + theAdjustedTicksPerRingBuffer;

theNextHostTime = gDevice_AnchorHostTime + ((UInt64)theHostTickOffset);
theNextHostTime = gDevice_AnchorHostTime + ((UInt64)theNextTickOffset);

// go to the next time if the next host time is less than the current time
if(theNextHostTime <= theCurrentHostTime)
{
++gDevice_NumberTimeStamps;
gDevice_PreviousTicks = theNextTickOffset;
}

// set the return values
*outSampleTime = gDevice_NumberTimeStamps * kDevice_RingBufferSize;
*outHostTime = gDevice_AnchorHostTime + (((Float64)gDevice_NumberTimeStamps) * theHostTicksPerRingBuffer);
*outHostTime = gDevice_AnchorHostTime + gDevice_PreviousTicks;
*outSeed = 1;

// DebugMsg("SampleTime: %f \t HostTime: %llu", *outSampleTime, *outHostTime);
Expand Down
2 changes: 2 additions & 0 deletions BlackHole/BlackHole.h
Expand Up @@ -138,6 +138,8 @@ static Float64 gDevice_SampleRate = 44100.
static UInt64 gDevice_IOIsRunning = 0;
static const UInt32 kDevice_RingBufferSize = 16384;
static Float64 gDevice_HostTicksPerFrame = 0.0;
static Float64 gDevice_AdjustedTicksPerFrame = 0.0;
static Float64 gDevice_PreviousTicks = 0.0;
static UInt64 gDevice_NumberTimeStamps = 0;
static Float64 gDevice_AnchorSampleTime = 0.0;
static UInt64 gDevice_AnchorHostTime = 0;
Expand Down

0 comments on commit d9f2b7a

Please sign in to comment.