Metronome for variable bpm and variable timeSignature

185 views
Skip to first unread message

Michiel Koorn

unread,
Mar 29, 2017, 2:12:53 PM3/29/17
to AudioKit Users Group
Hi,

I'm stuck with a metronome for my project.

I have seen the metronome example and got that working.

The problem is that I have an audio track wich can variate in bpm and in time signature.
The metronome I would like to play alongside the song needs to be adjusting to the changing bpm and changing time signatures.

I have tried working with multiple metronomes for the changing time signature and bpm's but got stuck.

Then I was thinking, since I know all timeSignatures and bpm changes before starting to play, 
Would it be possible to create a midi track in code with the metronome ticks on all the right beats etc. and play that along side the audio track? This way I can have the metronomes as complicated as I'd like.

I have been looking at AKMidi but cannot figure out where to start.

I am hoping someone can give me some feedback on this. Is this a good way to implement this metronome?
I imagine I can start a midi track like that at a certain time just as I can start the audio file at a certain time in the track and thus have both tracks synced.

If this is a good way forward, some sample code to get me started would be much appreciated.

Thanks and regards,
Michiel

Aurelius Prochazka Ph.D.

unread,
Apr 1, 2017, 1:42:21 AM4/1/17
to AudioKit Users Group
Synchronizing a metronome to an audio track sounds easy, but in practice it can be a bit painful.  If you do have an exactly accurate to the sample recording, the way you describe sounds reasonable.

Aure

Michiel Koorn

unread,
Apr 3, 2017, 3:52:28 PM4/3/17
to AudioKit Users Group
Hi Aure,

Thanks for you reply.

I have been trying to create a midi track with notes for each measure to create a metronome as described.

Using some code which I found in one of the posts here I tried this:

if let audioFileUrl = Bundle.main.url(forResource: "click", withExtension: "wav") {
           
           do {
               
               let audioFile = try AKAudioFile(forReading: audioFileUrl)
               self.midiSampler = audioFile.midiSampler
               
           } catch {
               NSLog("\(error)")
           }
       }
       
       
       let midi = AKMIDI()
       
       self.sequencer = AKSequencer(engine: AudioKit.engine)
       self.sequencer?.isAVSequencer = false
       self.midiSampler?.enableMIDI(midi.client, name: "midi sample")
       
       let track = self.sequencer?.newTrack()
       
       track?.setMIDIOutput((self.midiSampler?.midiIn)!)
       track?.setLength(AKDuration(beats: 64, tempo: 120))
       track?.add(noteNumber: 60, velocity: 127, position: AKDuration(beats:Double(1)), duration: AKDuration(beats:16.0), channel:1)
       
       self.mixer = AKMixer()
       self.mixer?.connect(self.midiSampler)
       AudioKit.output = self.mixer!
       AudioKit.start()
       
       self.sequencer?.play()
        
        
This fails miserably though.

I am hoping you can provide me with some example code for building up the midi track to get me started.

Also I wonder how I can sync the midi track with the audio file.
The audioPlayer I can play from beginTime to endTime. Can I do the same with a miditrack so that these would sync?

Thanks for your time.

Regards,
Michiel
Reply all
Reply to author
Forward
0 new messages