2016-01-04 23:12:47 -05:00
|
|
|
//
|
|
|
|
// CSMachine.m
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 04/01/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "CSMachine.h"
|
|
|
|
#import "CSMachine+Subclassing.h"
|
|
|
|
|
2016-01-13 22:38:59 -05:00
|
|
|
struct SpeakerDelegate: public Outputs::Speaker::Delegate {
|
|
|
|
__weak CSMachine *machine;
|
2016-01-14 20:33:22 -05:00
|
|
|
void speaker_did_complete_samples(Outputs::Speaker *speaker, const int16_t *buffer, int buffer_size) {
|
2016-01-13 22:38:59 -05:00
|
|
|
[machine speaker:speaker didCompleteSamples:buffer length:buffer_size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-04 23:12:47 -05:00
|
|
|
@implementation CSMachine {
|
2016-01-13 22:38:59 -05:00
|
|
|
SpeakerDelegate _speakerDelegate;
|
2016-01-04 23:12:47 -05:00
|
|
|
dispatch_queue_t _serialDispatchQueue;
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:33:22 -05:00
|
|
|
- (void)speaker:(Outputs::Speaker *)speaker didCompleteSamples:(const int16_t *)samples length:(int)length {
|
|
|
|
[self.audioQueue enqueueAudioBuffer:samples numberOfSamples:(unsigned int)length];
|
2016-01-13 22:38:59 -05:00
|
|
|
}
|
|
|
|
|
2016-01-04 23:12:47 -05:00
|
|
|
- (instancetype)init {
|
|
|
|
self = [super init];
|
|
|
|
|
2016-04-24 22:32:24 -04:00
|
|
|
if(self) {
|
2016-01-04 23:12:47 -05:00
|
|
|
_serialDispatchQueue = dispatch_queue_create("Machine queue", DISPATCH_QUEUE_SERIAL);
|
2016-01-13 22:38:59 -05:00
|
|
|
_speakerDelegate.machine = self;
|
|
|
|
[self setSpeakerDelegate:&_speakerDelegate sampleRate:44100];
|
2016-01-04 23:12:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-04-24 20:34:25 -04:00
|
|
|
- (void)dealloc {
|
|
|
|
[_view performWithGLContext:^{
|
|
|
|
[self closeOutput];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:38:59 -05:00
|
|
|
- (BOOL)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate sampleRate:(int)sampleRate {
|
|
|
|
return NO;
|
|
|
|
}
|
2016-03-19 22:46:17 -04:00
|
|
|
|
|
|
|
- (void)runForNumberOfCycles:(int)numberOfCycles {}
|
|
|
|
|
|
|
|
- (void)performSync:(dispatch_block_t)action {
|
|
|
|
dispatch_sync(_serialDispatchQueue, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)performAsync:(dispatch_block_t)action {
|
|
|
|
dispatch_async(_serialDispatchQueue, action);
|
|
|
|
}
|
2016-01-04 23:40:43 -05:00
|
|
|
|
2016-04-11 23:12:56 -04:00
|
|
|
- (void)setupOutputWithAspectRatio:(float)aspectRatio {}
|
2016-03-20 13:50:13 -04:00
|
|
|
|
2016-04-24 20:34:25 -04:00
|
|
|
- (void)closeOutput {}
|
|
|
|
|
2016-04-11 23:12:56 -04:00
|
|
|
- (void)setView:(CSOpenGLView *)view aspectRatio:(float)aspectRatio {
|
2016-04-24 20:34:25 -04:00
|
|
|
_view = view;
|
2016-03-20 13:50:13 -04:00
|
|
|
[view performWithGLContext:^{
|
2016-04-11 23:12:56 -04:00
|
|
|
[self setupOutputWithAspectRatio:aspectRatio];
|
2016-03-20 13:50:13 -04:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:12:47 -05:00
|
|
|
@end
|