4

Following on from a previous issue, I stopped using AKSampler to move to the functionality used in AKMIDISampler. Got my loops working again (with help from this Google Groups post), but I have a sinewave playing (which happens when the MIDISampler can't find it's source file).

It's not an issue with the source files I'm targeting because they all play OK. The sinewave is coming from somewhere else in the process, but I can't see where...

Please help 8•)

(Simplified and edited code to show only relevant details - please get in touch for any clarification)

var MIDISamplePlayer = AKMIDISampler()
var sequencer: AKSequencer?
var mixer: AKMixer!

// initialise the mixer
mixer = AKMixer()

do  {
        audiofile = try AKAudioFile(readFileName: SoundFilename as! String, 
        baseDir: .resources)
    } catch let error as NSError {
        print("There's an error: \(error)")
    }

do {
       try sprite.MIDISamplePlayer.loadAudioFile(audiofile)
    } catch let error as NSError {
       print("There's an error: \(error)")
    }

sprite.tracker = AKAmplitudeTracker(sprite.MIDISamplePlayer)
mixer.connect(to:sprite.tracker, bus: mixer.nextInput.bus)

sequencer = AKSequencer(filename: POPmidi)
sequencer?.enableLooping()

let midi = AKMIDI()

for i in 0..<popCount {
    gPOPs[i].MIDISamplePlayer.enableMIDI(midi.client, name: "MIDISample_\(i)")
    mixer.connect(gPOPs[i].MIDISamplePlayer)
    sequencer!.tracks[i].setMIDIOutput(gPOPs[i].MIDISamplePlayer.midiIn)
}

AudioKit.start()
sequencer!.play()
Mr_P
  • 523
  • 2
  • 16
  • 1
    I discovered one other case where the AVAudioUnitSampler plays a sine wave (on iOS). If you don't setup your plist for background audio, the Sampler will succeed at loading your samples, but still play the default sine wave. – Eric George Nov 14 '17 at 03:14
  • Thanks for this input Eric. I already have background music enabled: ``Required background modes: App plays audio or streams audio/video using AirPlay`` which is the setting I presume. I'm going to investigate the AVAudioUnitSampler now as a possible source – Mr_P Nov 14 '17 at 12:10
  • Could it come from the tempo track as you described in https://stackoverflow.com/questions/47335779/issues-with-extra-midi-track-being-created-in-aksequencer-using-audiokit? – mahal tertin Nov 16 '17 at 19:40
  • Thanks Mahal, your suggestion led to my eventual discovery of the issue here, see below... – Mr_P Nov 17 '17 at 22:04

4 Answers4

2

I found out the cause of this issue. I learnt that the MIDI sequencer was creating an extra track when I gave it a specific number of MIDI tracks; for four tracks in my midi file, the sequencer was giving me five. This extra track is for tempo (as discovered here).

I also learnt that my first track was being ignored from my .plist (where I set up the stems for different songs).

My assumption that the first track is being used for the tempo track was tested, and when I created five tracks in my .plist to match the sequencer, the sinewave tone disappeared.

This lead to a workaround hack. I did this by duplicating the first sample in my .plist, which was being ignored anyway, and then hiding the sprite that represented this sound off screen. This works for now, but if anyone has an idea of how to control this tempo track and its use in AKSequencer I’d love to know more.

So in short: Make sure that the MIDI file has exact number of tracks that you want to use. Create the same number of AKMIDISampler to use in AKSequencer adding one for tempo track. Then beware that the first track in the sequencer is the tempo track and will not work playing samples.

Mr_P
  • 523
  • 2
  • 16
1

This is a bit of a guess, but its a very common issue to have your audio files in a location that the sampler likes. Try putting the audiofiles in a Samples/ folder like in these examples:

http://audiokit.io/playgrounds/Playback/Sequencer/ http://audiokit.io/playgrounds/Playback/Sampler/

or I think a Sounds or "Sampler Instruments" folders work as well as in the Sampler Demo:

https://github.com/AudioKit/AudioKit/tree/master/Examples/iOS/SamplerDemo/SamplerDemo/Sounds

Aurelius Prochazka
  • 4,510
  • 2
  • 11
  • 34
  • I’ll try this in case it helps. All mp3s are actually loading and playing as expected, except for the first one it attempts (which looks like it is loaded in console, but which can’t be triggered)... – Mr_P Nov 16 '17 at 18:13
1

I had a mystery sinewave in my code as well. In my situation, it turns out I had an extra occurrence of the following code:

AudioKit.output = sampler

So I accidentally ended up specifying AudioKit.output twice. After removing the extra occurrence, the sinewave disappeared. I'm not sure how this caused the sinewave, but I'm leaving this answer here in case it helps others who might have a similar issue.

eggangel
  • 11
  • 4
0

Another way to get this mystery sinewave is by using the normal approach to setting preferredSampleRate

//   try session.setPreferredSampleRate(preferredSampleRate)

in AppDelegate for Audio Session... instead of setting it in the AudioKit AKSettings, it will produce the mystery sinewave midi synth...

KorinW
  • 279
  • 4
  • 10