1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-01-11 08:30:55 +00:00

Closed the loop such that audio manages to bubble up into Objective-C.

This commit is contained in:
Thomas Harte 2016-01-13 22:38:59 -05:00
parent 439d452e23
commit afde8dac49
5 changed files with 27 additions and 8 deletions

View File

@ -7,6 +7,7 @@
//
import Foundation
import AudioToolbox
class ElectronDocument: MachineDocument {

View File

@ -35,9 +35,11 @@
_electron.get_crt()->set_delegate(delegate);
}
- (void)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate {
_electron.get_speaker()->set_output_rate(44100, 512);
- (BOOL)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate sampleRate:(int)sampleRate {
_electron.get_speaker()->set_output_rate(sampleRate, 512);
_electron.get_speaker()->set_output_quality(15);
_electron.get_speaker()->set_delegate(delegate);
return YES;
}
- (void)setView:(CSCathodeRayView *)view {

View File

@ -12,12 +12,13 @@
@interface CSMachine (Subclassing)
- (void)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate;
- (BOOL)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate sampleRate:(int)sampleRate;
- (void)setCRTDelegate:(Outputs::CRT::Delegate *)delegate;
- (void)doRunForNumberOfCycles:(int)numberOfCycles;
- (void)perform:(dispatch_block_t)action;
- (void)crt:(Outputs::CRT *)crt didEndFrame:(CRTFrame *)frame didDetectVSync:(BOOL)didDetectVSync;
- (void)speaker:(Outputs::Speaker *)speaker didCompleteSamples:(const uint16_t *)samples length:(int)length;
@end

View File

@ -16,6 +16,13 @@ struct CRTDelegate: public Outputs::CRT::Delegate {
}
};
struct SpeakerDelegate: public Outputs::Speaker::Delegate {
__weak CSMachine *machine;
void speaker_did_complete_samples(Outputs::Speaker *speaker, const uint16_t *buffer, int buffer_size) {
[machine speaker:speaker didCompleteSamples:buffer length:buffer_size];
}
};
typedef NS_ENUM(NSInteger, CSAtari2600RunningState) {
CSMachineRunningStateRunning,
CSMachineRunningStateStopped
@ -23,6 +30,7 @@ typedef NS_ENUM(NSInteger, CSAtari2600RunningState) {
@implementation CSMachine {
CRTDelegate _crtDelegate;
SpeakerDelegate _speakerDelegate;
dispatch_queue_t _serialDispatchQueue;
NSConditionLock *_runningLock;
@ -36,6 +44,9 @@ typedef NS_ENUM(NSInteger, CSAtari2600RunningState) {
if([self.view pushFrame:frame]) crt->return_frame();
}
- (void)speaker:(Outputs::Speaker *)speaker didCompleteSamples:(const uint16_t *)samples length:(int)length {
}
- (void)runForNumberOfCycles:(int)cycles {
if([_runningLock tryLockWhenCondition:CSMachineRunningStateStopped]) {
[_runningLock unlockWithCondition:CSMachineRunningStateRunning];
@ -51,18 +62,22 @@ typedef NS_ENUM(NSInteger, CSAtari2600RunningState) {
self = [super init];
if (self) {
_crtDelegate.machine = self;
[self setCRTDelegate:&_crtDelegate];
[self setSpeakerDelegate:nil];
_serialDispatchQueue = dispatch_queue_create("Machine queue", DISPATCH_QUEUE_SERIAL);
_runningLock = [[NSConditionLock alloc] initWithCondition:CSMachineRunningStateStopped];
_crtDelegate.machine = self;
_speakerDelegate.machine = self;
[self setCRTDelegate:&_crtDelegate];
[self setSpeakerDelegate:&_speakerDelegate sampleRate:44100];
}
return self;
}
- (void)setCRTDelegate:(Outputs::CRT::Delegate *)delegate {}
- (void)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate {}
- (BOOL)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate sampleRate:(int)sampleRate {
return NO;
}
- (void)doRunForNumberOfCycles:(int)numberOfCycles {}
@end

View File

@ -19,7 +19,7 @@ class Speaker {
public:
class Delegate {
public:
virtual void speaker_did_complete_samples(Speaker *speaker, const uint16_t *buffer, int buffer_size);
virtual void speaker_did_complete_samples(Speaker *speaker, const uint16_t *buffer, int buffer_size) = 0;
};
void set_output_rate(int cycles_per_second, int buffer_size)