1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-02-14 04:31:19 +00:00

Provide sample length ahead of time.

This commit is contained in:
Thomas Harte 2022-07-14 14:34:11 -04:00
parent 5aa129fbd3
commit ddfc2e4ca4
3 changed files with 12 additions and 5 deletions

View File

@ -36,9 +36,8 @@
Enqueues a buffer for playback. Enqueues a buffer for playback.
@param buffer A pointer to the data that comprises the buffer. @param buffer A pointer to the data that comprises the buffer.
@param lengthInSamples The length of the buffer, in samples.
*/ */
- (void)enqueueAudioBuffer:(nonnull const int16_t *)buffer numberOfSamples:(size_t)lengthInSamples; - (void)enqueueAudioBuffer:(nonnull const int16_t *)buffer;
/// @returns The sampling rate at which this queue is playing audio. /// @returns The sampling rate at which this queue is playing audio.
@property (nonatomic, readonly) Float64 samplingRate; @property (nonatomic, readonly) Float64 samplingRate;
@ -58,6 +57,11 @@
*/ */
@property (nonatomic, readonly) NSUInteger preferredBufferSize; @property (nonatomic, readonly) NSUInteger preferredBufferSize;
/*!
Sets the size of buffers to be posted, in samplrs.
*/
@property (nonatomic) NSUInteger bufferSize;
/*! /*!
@returns @C YES if this queue is running low or is completely exhausted of new audio buffers. @returns @C YES if this queue is running low or is completely exhausted of new audio buffers.
*/ */

View File

@ -119,8 +119,8 @@
#pragma mark - Audio enqueuer #pragma mark - Audio enqueuer
- (void)enqueueAudioBuffer:(const int16_t *)buffer numberOfSamples:(size_t)lengthInSamples { - (void)enqueueAudioBuffer:(const int16_t *)buffer {
size_t bufferBytes = lengthInSamples * sizeof(int16_t); const size_t bufferBytes = self.bufferSize * sizeof(int16_t);
// Don't enqueue more than 4 buffers ahead of now, to ensure not too much latency accrues. // Don't enqueue more than 4 buffers ahead of now, to ensure not too much latency accrues.
if(atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed) == 4) { if(atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed) == 4) {

View File

@ -30,6 +30,7 @@
#import "NSBundle+DataResource.h" #import "NSBundle+DataResource.h"
#import "NSData+StdVector.h" #import "NSData+StdVector.h"
#include <cassert>
#include <atomic> #include <atomic>
#include <bitset> #include <bitset>
#include <codecvt> #include <codecvt>
@ -175,7 +176,8 @@ struct ActivityObserver: public Activity::Observer {
} }
- (void)speaker:(Outputs::Speaker::Speaker *)speaker didCompleteSamples:(const int16_t *)samples length:(int)length { - (void)speaker:(Outputs::Speaker::Speaker *)speaker didCompleteSamples:(const int16_t *)samples length:(int)length {
[self.audioQueue enqueueAudioBuffer:samples numberOfSamples:(unsigned int)length]; assert(NSUInteger(length) == self.audioQueue.bufferSize);
[self.audioQueue enqueueAudioBuffer:samples];
} }
- (void)speakerDidChangeInputClock:(Outputs::Speaker::Speaker *)speaker { - (void)speakerDidChangeInputClock:(Outputs::Speaker::Speaker *)speaker {
@ -231,6 +233,7 @@ struct ActivityObserver: public Activity::Observer {
- (void)setAudioSamplingRate:(float)samplingRate bufferSize:(NSUInteger)bufferSize stereo:(BOOL)stereo { - (void)setAudioSamplingRate:(float)samplingRate bufferSize:(NSUInteger)bufferSize stereo:(BOOL)stereo {
@synchronized(self) { @synchronized(self) {
self.audioQueue.bufferSize = bufferSize;
[self setSpeakerDelegate:&_speakerDelegate sampleRate:samplingRate bufferSize:bufferSize stereo:stereo]; [self setSpeakerDelegate:&_speakerDelegate sampleRate:samplingRate bufferSize:bufferSize stereo:stereo];
} }
} }