mirror of
https://github.com/TomHarte/CLK.git
synced 2025-08-13 00:25:26 +00:00
Closed the loop such that audio manages to bubble up into Objective-C.
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
import AudioToolbox
|
||||||
|
|
||||||
class ElectronDocument: MachineDocument {
|
class ElectronDocument: MachineDocument {
|
||||||
|
|
||||||
|
@@ -35,9 +35,11 @@
|
|||||||
_electron.get_crt()->set_delegate(delegate);
|
_electron.get_crt()->set_delegate(delegate);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate {
|
- (BOOL)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate sampleRate:(int)sampleRate {
|
||||||
_electron.get_speaker()->set_output_rate(44100, 512);
|
_electron.get_speaker()->set_output_rate(sampleRate, 512);
|
||||||
_electron.get_speaker()->set_output_quality(15);
|
_electron.get_speaker()->set_output_quality(15);
|
||||||
|
_electron.get_speaker()->set_delegate(delegate);
|
||||||
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setView:(CSCathodeRayView *)view {
|
- (void)setView:(CSCathodeRayView *)view {
|
||||||
|
@@ -12,12 +12,13 @@
|
|||||||
|
|
||||||
@interface CSMachine (Subclassing)
|
@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)setCRTDelegate:(Outputs::CRT::Delegate *)delegate;
|
||||||
|
|
||||||
- (void)doRunForNumberOfCycles:(int)numberOfCycles;
|
- (void)doRunForNumberOfCycles:(int)numberOfCycles;
|
||||||
- (void)perform:(dispatch_block_t)action;
|
- (void)perform:(dispatch_block_t)action;
|
||||||
|
|
||||||
- (void)crt:(Outputs::CRT *)crt didEndFrame:(CRTFrame *)frame didDetectVSync:(BOOL)didDetectVSync;
|
- (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
|
@end
|
||||||
|
@@ -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) {
|
typedef NS_ENUM(NSInteger, CSAtari2600RunningState) {
|
||||||
CSMachineRunningStateRunning,
|
CSMachineRunningStateRunning,
|
||||||
CSMachineRunningStateStopped
|
CSMachineRunningStateStopped
|
||||||
@@ -23,6 +30,7 @@ typedef NS_ENUM(NSInteger, CSAtari2600RunningState) {
|
|||||||
|
|
||||||
@implementation CSMachine {
|
@implementation CSMachine {
|
||||||
CRTDelegate _crtDelegate;
|
CRTDelegate _crtDelegate;
|
||||||
|
SpeakerDelegate _speakerDelegate;
|
||||||
|
|
||||||
dispatch_queue_t _serialDispatchQueue;
|
dispatch_queue_t _serialDispatchQueue;
|
||||||
NSConditionLock *_runningLock;
|
NSConditionLock *_runningLock;
|
||||||
@@ -36,6 +44,9 @@ typedef NS_ENUM(NSInteger, CSAtari2600RunningState) {
|
|||||||
if([self.view pushFrame:frame]) crt->return_frame();
|
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 {
|
- (void)runForNumberOfCycles:(int)cycles {
|
||||||
if([_runningLock tryLockWhenCondition:CSMachineRunningStateStopped]) {
|
if([_runningLock tryLockWhenCondition:CSMachineRunningStateStopped]) {
|
||||||
[_runningLock unlockWithCondition:CSMachineRunningStateRunning];
|
[_runningLock unlockWithCondition:CSMachineRunningStateRunning];
|
||||||
@@ -51,18 +62,22 @@ typedef NS_ENUM(NSInteger, CSAtari2600RunningState) {
|
|||||||
self = [super init];
|
self = [super init];
|
||||||
|
|
||||||
if (self) {
|
if (self) {
|
||||||
_crtDelegate.machine = self;
|
|
||||||
[self setCRTDelegate:&_crtDelegate];
|
|
||||||
[self setSpeakerDelegate:nil];
|
|
||||||
_serialDispatchQueue = dispatch_queue_create("Machine queue", DISPATCH_QUEUE_SERIAL);
|
_serialDispatchQueue = dispatch_queue_create("Machine queue", DISPATCH_QUEUE_SERIAL);
|
||||||
_runningLock = [[NSConditionLock alloc] initWithCondition:CSMachineRunningStateStopped];
|
_runningLock = [[NSConditionLock alloc] initWithCondition:CSMachineRunningStateStopped];
|
||||||
|
|
||||||
|
_crtDelegate.machine = self;
|
||||||
|
_speakerDelegate.machine = self;
|
||||||
|
[self setCRTDelegate:&_crtDelegate];
|
||||||
|
[self setSpeakerDelegate:&_speakerDelegate sampleRate:44100];
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setCRTDelegate:(Outputs::CRT::Delegate *)delegate {}
|
- (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 {}
|
- (void)doRunForNumberOfCycles:(int)numberOfCycles {}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@@ -19,7 +19,7 @@ class Speaker {
|
|||||||
public:
|
public:
|
||||||
class Delegate {
|
class Delegate {
|
||||||
public:
|
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)
|
void set_output_rate(int cycles_per_second, int buffer_size)
|
||||||
|
Reference in New Issue
Block a user