2016-01-05 04:12:47 +00: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-06-01 02:16:20 +00:00
|
|
|
@interface CSMachine()
|
|
|
|
- (void)speaker:(Outputs::Speaker *)speaker didCompleteSamples:(const int16_t *)samples length:(int)length;
|
|
|
|
@end
|
|
|
|
|
2016-01-14 03:38:59 +00:00
|
|
|
struct SpeakerDelegate: public Outputs::Speaker::Delegate {
|
|
|
|
__weak CSMachine *machine;
|
2016-01-15 01:33:22 +00:00
|
|
|
void speaker_did_complete_samples(Outputs::Speaker *speaker, const int16_t *buffer, int buffer_size) {
|
2016-01-14 03:38:59 +00:00
|
|
|
[machine speaker:speaker didCompleteSamples:buffer length:buffer_size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-05 04:12:47 +00:00
|
|
|
@implementation CSMachine {
|
2016-01-14 03:38:59 +00:00
|
|
|
SpeakerDelegate _speakerDelegate;
|
2016-01-05 04:12:47 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 01:33:22 +00:00
|
|
|
- (void)speaker:(Outputs::Speaker *)speaker didCompleteSamples:(const int16_t *)samples length:(int)length {
|
|
|
|
[self.audioQueue enqueueAudioBuffer:samples numberOfSamples:(unsigned int)length];
|
2016-01-14 03:38:59 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:34:25 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
[_view performWithGLContext:^{
|
2016-06-01 02:16:20 +00:00
|
|
|
@synchronized(self) {
|
|
|
|
self.machine->close_output();
|
|
|
|
}
|
2016-04-25 00:34:25 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
- (int)idealSamplingRateFromRange:(NSRange)range {
|
|
|
|
@synchronized(self) {
|
|
|
|
Outputs::Speaker *speaker = self.machine->get_speaker();
|
|
|
|
if(speaker)
|
|
|
|
{
|
|
|
|
return speaker->get_ideal_clock_rate_in_range((int)range.location, (int)(range.location + range.length));
|
|
|
|
}
|
2016-06-05 15:20:05 +00:00
|
|
|
return 0;
|
2016-06-01 23:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAudioSamplingRate:(int)samplingRate {
|
|
|
|
@synchronized(self) {
|
|
|
|
_speakerDelegate.machine = self;
|
|
|
|
[self setSpeakerDelegate:&_speakerDelegate sampleRate:samplingRate];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 03:38:59 +00:00
|
|
|
- (BOOL)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate sampleRate:(int)sampleRate {
|
2016-06-01 02:16:20 +00:00
|
|
|
@synchronized(self) {
|
|
|
|
Outputs::Speaker *speaker = self.machine->get_speaker();
|
|
|
|
if(speaker)
|
|
|
|
{
|
2016-06-02 23:50:16 +00:00
|
|
|
speaker->set_output_rate(sampleRate, 512);
|
2016-06-01 02:16:20 +00:00
|
|
|
speaker->set_delegate(delegate);
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
2016-01-14 03:38:59 +00:00
|
|
|
}
|
2016-03-20 02:46:17 +00:00
|
|
|
|
2016-06-01 02:16:20 +00:00
|
|
|
- (void)runForNumberOfCycles:(int)numberOfCycles {
|
|
|
|
@synchronized(self) {
|
|
|
|
self.machine->run_for_cycles(numberOfCycles);
|
|
|
|
}
|
|
|
|
}
|
2016-03-20 02:46:17 +00:00
|
|
|
|
2016-04-12 03:12:56 +00:00
|
|
|
- (void)setView:(CSOpenGLView *)view aspectRatio:(float)aspectRatio {
|
2016-04-25 00:34:25 +00:00
|
|
|
_view = view;
|
2016-03-20 17:50:13 +00:00
|
|
|
[view performWithGLContext:^{
|
2016-04-12 03:12:56 +00:00
|
|
|
[self setupOutputWithAspectRatio:aspectRatio];
|
2016-03-20 17:50:13 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2016-06-01 02:16:20 +00:00
|
|
|
- (void)setupOutputWithAspectRatio:(float)aspectRatio {
|
|
|
|
self.machine->setup_output(aspectRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawViewForPixelSize:(CGSize)pixelSize onlyIfDirty:(BOOL)onlyIfDirty {
|
|
|
|
self.machine->get_crt()->draw_frame((unsigned int)pixelSize.width, (unsigned int)pixelSize.height, onlyIfDirty ? true : false);
|
|
|
|
}
|
|
|
|
|
2016-01-05 04:12:47 +00:00
|
|
|
@end
|