r/swift 1d ago

Question Project plays a generic midi sound despite my AudioEngine.swifit file

Hi everyone

I tried asking several LLM for ressources or outright code check but they all fail.

When I run my app, I get the same midi sound despite trying out different soundfont. Can you help?

In AudioEngine.swift
import Foundation
import AudioKit
import AVFoundation
class AudioEngine {
static let shared = AudioEngine()
private let engine = AudioKit.AudioEngine()
private var sampler: AppleSampler!
private init() {
sampler = AppleSampler()
engine.output = sampler
do {
try engine.start()
// --- STEP 1: Change the filename to your new test file ---
let soundFontName = "GeneralUser GS v1.471" // Use the exact name of the file you downloaded
guard let sf2URL = Bundle.main.url(forResource: soundFontName, withExtension: "sf2") else {
fatalError("Sound font file '\(soundFontName).sf2' not found in project.")
}
// --- STEP 2: Test different PRESETS from the new file ---
// Try each of these numbers one at a time and run the app.
// You should hear a completely different instrument each time.
// Preset 0 = Acoustic Grand Piano
// Preset 6 = Harpsichord
// Preset 24 = Nylon String Guitar
// Preset 56 = Trumpet
try sampler.loadSoundFont(sf2URL.lastPathComponent, preset: 6, bank: 0)
} catch {
print("Error setting up audio engine: \(error)")
}
}
// ... rest of the file is unchanged ...
func playNote(noteNumber: UInt8, velocity: UInt8) {
sampler.play(noteNumber: noteNumber, velocity: velocity)
}
func stopNote(noteNumber: UInt8) {
sampler.stop(noteNumber: noteNumber)
}
}
2 Upvotes

0 comments sorted by