strummachine / sm-audiokit

0 stars 0 forks source link

High CPU use with mono audio files #6

Closed banjerluke closed 2 years ago

banjerluke commented 2 years ago

I noticed that if I put mono MP3 files in the audio-package, the CPU bogs down (to the point of unresponsiveness) with lots of the following error in the console:

AudioPlayer+Buffering.swift:updateBuffer(force:):68:Copying mono data to 2 channel buffer... <AVAudioFormat 0x6000022354f0:  1 ch,  44100 Hz, Float32> (AudioPlayer+Buffering.swift:updateBuffer(force:):68)

This happens every time a file is loaded scheduled for playback, even if it's already loaded and buffered! AudioPlayer+Scheduling.swift:16 calls AudioPlayer.updateBuffer(), I guess to reload the file from disk in case it's changed? I checked, and indeed the buffer is available just before the call to AudioPlayer.play()...

This is a bummer because my samples are mono, and switching to stereo MP3 files means a big increase in file sizes – not quite 2x but still painful. Can you think of any way around this?

banjerluke commented 2 years ago

Welp, I fixed it:

-      try self.player.load(url: sample.url, buffered: true)
+      try self.player.load(url: sample.url)

This eliminates ALL of those console messages, and drops CPU use down to the same (kinda high) levels as when using stereo files.