Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from liyihann/main
Add Guitar support
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
45
Tests/MusicalInstrumentTests/Instruments/GuitarTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |