Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using an audio file for the sound #5

Open
MilesV64 opened this issue Dec 27, 2020 · 0 comments
Open

Using an audio file for the sound #5

MilesV64 opened this issue Dec 27, 2020 · 0 comments

Comments

@MilesV64
Copy link

MilesV64 commented Dec 27, 2020

Thanks for making this! I'm interested in using an audio file for the click sound rather than a generated triangle wave, but it doesn't seem to work.

The change (also using only one sound rather than 2):

    let clickURL = Bundle.main.url(forResource: "metronome.click", withExtension: "wav")!
    let audioFile = try! AVAudioFile(forReading: clickURL)
    let audioFormat = audioFile.processingFormat
    let audioFrameCount = UInt32(audioFile.length)

    audioBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)
    audioBuffer.frameLength = audioFrameCount
    try! audioFile.read(into: audioBuffer!)
    
    self.audioFormat = buffers[0]!.format
    self.bufferSampleRate = audioFormat.sampleRate
    
    initiazeDefaults()

    engine.attach(player)
    engine.connect(player, to:  engine.outputNode, fromBus: 0, toBus: 0, format: audioFormat)

This works perfectly at BPMs below 90 or so, but as the BPM increases the clicks get quieter and quieter until they disappear. Any thoughts on how to support audio files?

EDIT: Sound file works if it's 0.02 seconds in duration or less it seems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant