0

First of all, a great framework. This is singlehandedly allowing me to graduate from my master's program. Also, I'm a sponsor! Any help would be taken with much gratitude. I can also push my repository and share it on GitHub for a closer look.

Anyway, here is my code

import Foundation
import AudioKit

class DrumSounds {

    let drums = AKMIDISampler()
    var currentBPM = 60
    var rideCymbalFile: AKAudioFile?
    var snareDrumFile: AKAudioFile?
    var bassDrumFile: AKAudioFile?
    var hiHatFile: AKAudioFile?
    let sequencer = AKAppleSequencer(filename: "4tracks")
    var booster = AKBooster()
    init() {

        do{

        try rideCymbalFile = AKAudioFile(readFileName: "rideCymbalSound.wav")
        try snareDrumFile = AKAudioFile(readFileName: "snareDrumSound.wav")
        try bassDrumFile = AKAudioFile(readFileName: "bassDrumSound.wav")
        try hiHatFile = AKAudioFile(readFileName: "hiHatSound.mp3")
        try drums.loadAudioFiles([rideCymbalFile!,
                                   snareDrumFile!,
                                   bassDrumFile!,
                                   hiHatFile!])

        } catch {
            print("error loading samples to drum object")
        }

        drums.volume = 1
        booster = AKBooster(drums)
        AudioKit.output = drums
        sequencer.clearRange(start: AKDuration(beats: 0), duration: AKDuration(beats: 100))
        sequencer.debug()
        sequencer.setGlobalMIDIOutput(drums.midiIn)
        sequencer.enableLooping(AKDuration(beats: 4))
        sequencer.setTempo(Double(currentBPM))

    }

    func playDrumSounds () {

        do {
            try AKSettings.setSession(category: .playAndRecord, with:  AVAudioSession.CategoryOptions.defaultToSpeaker)

            let session = AVAudioSession.sharedInstance()
            try session.setCategory(AVAudioSession.Category.playAndRecord)

            if !AKSettings.headPhonesPlugged {
                try session.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
         }
        }catch {
            print("error in settings.setSession")
        }

        sequencer.tracks[0].add(noteNumber: 0, velocity: 127, position: AKDuration(beats: 0), duration: AKDuration(beats: 1.0))
        sequencer.tracks[0].add(noteNumber: 0, velocity: 127, position: AKDuration(beats: 1), duration: AKDuration(beats: 1.0))
        sequencer.tracks[0].add(noteNumber: 0, velocity: 127, position: AKDuration(beats: 2), duration: AKDuration(beats: 1.0))
        sequencer.tracks[0].add(noteNumber: 0, velocity: 127, position: AKDuration(beats: 3), duration: AKDuration(beats: 1.0))
        sequencer.play()

    }   
}
Marcus Kim
  • 283
  • 2
  • 12

1 Answers1

1

I figured it out by randomly stumbling upon a comment in another post. The volume is low because you need to enable "Audio, AirPlay, and Picture in Picture" in "Background Modes" under "Signing & Capabilities". Click the "+" button in the top left to add a capability: enter image description here

As for playing the right drum sound: The right drum sound was, in fact, being played. However I set the MIDI note number too low, so it sounded like harsh static. If you're having this problem and have never worked with MIDI (like me), here is a link to a description of MIDI note numbers: https://www.inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies. The higher the number, the higher the frequency. Changing the MIDI note number will change the frequency of your audio file!

Marcus Kim
  • 283
  • 2
  • 12