In Apple sample code, iOS Metronome , https://developer.apple.com/library/content/samplecode/HelloMetronome/Introduction/Intro.html#//apple_ref/doc/uid/TP40017587
Now Apple hard code self.setTempo(120) with 120 at the end of following code.
override init() {
super.init()
let format = AVAudioFormat(standardFormatWithSampleRate: 44100.0, channels: 2)
let bipFrames: UInt32 = UInt32(GlobalConstants.kBipDurationSeconds * Float(format.sampleRate))
soundBuffer.append(AVAudioPCMBuffer(pcmFormat: format, frameCapacity: bipFrames))
soundBuffer.append(AVAudioPCMBuffer(pcmFormat: format, frameCapacity: bipFrames))
soundBuffer[0]?.frameLength = bipFrames
soundBuffer[1]?.frameLength = bipFrames
let wg1 = TriangleWaveGenerator(sampleRate: Float(format.sampleRate))
let wg2 = TriangleWaveGenerator(sampleRate: Float(format.sampleRate), frequency: 261.6)
wg1.render(soundBuffer[0]!)
wg2.render(soundBuffer[1]!)
let output: AVAudioOutputNode = engine.outputNode
engine.attach(player)
engine.connect(player, to: output, fromBus: 0, toBus: 0, format: format)
bufferSampleRate = format.sampleRate
syncQueue = DispatchQueue(label: "Metronome")
self.setTempo(120)
}
How to pass parameters, instead of hardcoded with 120, from Objective C 's following code to above Swift code within init:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/
NSLog(@"Hello, Metronome!\n");
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient error:&error];
if (error) {
NSLog(@"AVAudioSession error %ld, %@", error.code, error.localizedDescription);
}
[audioSession setActive:YES error:&error];
if (error) {
NSLog(@"AVAudioSession error %ld, %@", error.code, error.localizedDescription);
}
/
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleMediaServicesWereReset:)
name:AVAudioSessionMediaServicesWereResetNotification
object:audioSession];
metronome = [[Metronome alloc] init];
metronome.delegate = self;
}
BTW When I run this sample at the first time with device, it begin with some tips of sound noise , then stop. Then it can work well from the second run. thanks so much!