Skip to content

Commit

Permalink
Merge pull request #1 from liyihann/main
Browse files Browse the repository at this point in the history
Add Guitar support
  • Loading branch information
live77 committed Mar 23, 2023
2 parents 0b09e7d + 6d31de5 commit 1fc81b0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/MusicalInstrument/AudioEngine/AudioEngine.swift
Expand Up @@ -64,6 +64,7 @@ public class AudioEngine {
private let melodicBank: UInt8 = UInt8(kAUSampler_DefaultMelodicBankMSB)
private let gmPiano: UInt8 = 2
private let gmViolin: UInt8 = 41
private let gmGuitar: UInt8 = 25

/// 录制的声音文件
private var file: AVAudioFile?
Expand Down Expand Up @@ -102,6 +103,7 @@ extension AudioEngine {
private func createSamplers(soundBankUrl: URL) -> Bool {
let pianoSampler = AVAudioUnitSampler()
let violinSampler = AVAudioUnitSampler()
let guitarSampler = AVAudioUnitSampler()

do {
try pianoSampler.loadSoundBankInstrument(at: soundBankUrl,
Expand All @@ -113,9 +115,14 @@ extension AudioEngine {
program: gmViolin,
bankMSB: melodicBank,
bankLSB: 0)
try guitarSampler.loadSoundBankInstrument(at: soundBankUrl,
program: gmGuitar,
bankMSB: melodicBank,
bankLSB: 0)

samplerMap[.piano] = pianoSampler
samplerMap[.strings] = violinSampler
samplerMap[.guitar] = guitarSampler

return true
} catch {
Expand Down
57 changes: 57 additions & 0 deletions Sources/MusicalInstrument/Instruments/Guitar.swift
@@ -0,0 +1,57 @@
//
// File.swift
//
//
// Created by liyihann on 2/22/23.
//
//

import Foundation
import MusicSymbol
import AVFAudio

// guitar
public class Guitar: MusicalInstrument {

@discardableResult public func play(at pitch: Pitch, with option: NotePlayingOption) -> MusicalInstrument {
controller.play(at: pitch, with: option.noteOnVelocity)
return self
}

@discardableResult public func stop(at pitch: Pitch) -> MusicalInstrument {
controller.stop(at: pitch)
return self
}

public func adjust(with option: InstrumentControlOption) {
// not supported yet
assertionFailure("Not supported yet")
}

public func stopAll() {
controller.stopAll()
}

public func name() -> String {
return "Guitar"
}

public func type() -> InstrumentFamily {
return .guitar
}

public init(sampler: AVAudioUnitSampler) {
self.controller = SamplerController(sampler: sampler)
}

/// default, easy to use
public static var `default` : Guitar = {
let engine = AudioEngine.default
let sampler = engine.sampler(.guitar)!
return Guitar(sampler: sampler)
}()

/// private
private let controller: SamplerController
}

45 changes: 45 additions & 0 deletions Tests/MusicalInstrumentTests/Instruments/GuitarTests.swift
@@ -0,0 +1,45 @@
//
// GuitarTests.swift
//
//
// Created by liyihann on 2/22/23.
//
//

import XCTest
import MusicSymbol
import MusicalInstrument

final class GuitarTests: XCTestCase {

// test default guitar
func testGuitarDefault() {
let guitar = Guitar.default
guitar.play(at: "C4", with: .velocity(60))
Thread.sleep(forTimeInterval: 1.0)
guitar.stopAll()
}

// test guitar chord
func testGuitarChord() {
let guitar = Guitar.default
guitar.play(at: "C4", with: .velocity(60))
guitar.play(at: "E4", with: .velocity(60))
guitar.play(at: "G4", with: .velocity(60))
Thread.sleep(forTimeInterval: 1.0)
guitar.stopAll()
}

// test play guitar and stop
func testStopAll() {
let guitar = Guitar.default

guitar.play(at: Pitch("C4"), with: .velocity(60))
guitar.play(at: Pitch("E4"), with: .velocity(60))

sleep(1)
guitar.stopAll()
sleep(2)
}

}

0 comments on commit 1fc81b0

Please sign in to comment.