Swift change Pitch and Speed of recorded audio - ios

I have an app who's foundation is essentially based on https://blckbirds.com/post/voice-recorder-app-in-swiftui-1/.
It's Swift / XCode 12.5.1 and works great. I call the audio using self.audioPlayer.startPlayback(audio: self.audioURL) which plays the recording perfectly.
Now I want to add the ability for the user to adjust the pitch and speed of the recorded audio. It doesn't have to save the changes, just apply the changes while playing the file on the fly.
I found https://www.hackingwithswift.com/example-code/media/how-to-control-the-pitch-and-speed-of-audio-using-avaudioengine which simplifies the process of applying pitch changes. I'm able to change the startPlayback above to
self.audioPlayer.speedControl.rate = 0.5
do {
try self.audioPlayer.play(self.audioURL)
}
catch let error as NSError {
print(error.localizedDescription)
}
after adding HWS's code into the AudioPlayer class, which proves it's working, but it's not an implementation.. it breaks some of the other capabilities (like updating and using the stopPlayback function), which I think is due to switching between the AVAudioPlayer and the AVAudioPlayerNode I'm trying to figure out if I need to rewrite the AudioPlayer.swift from the blckbirds tutorial, or if there's a friendlier way to incorporate HWS's into the project.
For example, I suppose I could create a toggle that would use the AVAudioPlayer playback if no effects are being used, then if the toggle enables one of the effects, have it use AVAudioPlayerNode instead.. but that seems inefficient. I'd appreciate any thoughts here!

Turns out this was simpler than I had thought using #AppStorage and conditionals to integrate the desired player. Thanks!

Related

Best way to play silence using AVAudioPlayer on iOS

I found myself in a situation where I need to simulate audio playback to trick OS controls and MPNowPlayingInfoCenter into thinking that an audio is being played. This is because I am building a player that plays multiple audio tracks, with pauses in-between creating one, continuous "audio" track. I have already everything setup inside the app itself, and the lock screen controls are working correctly but the only problem I am facing is while the actual audio stops and a pause is being "played", the lock screen info center stops the timer, and it only continues with showing correct time and overall state once another audio track starts playing.
Here is the example of my audio track built from audio files and pause items:
let items: [AudioItem] = [
.audio("part-1.mp3"),
.pause(duration: 5), // value of type: TimeInterval
.audio("part-2.mp3"),
.pause(duration: 3),
... // the list goes on
]
then in my custom player, once AVAudioPlayer finishes its job with current item, I get the next one from the array and play either a .pause with a scheduled Timer or another .audio with AVAudioPlayer.
extension Player: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
playNextItem()
}
}
And here lies the problem, once the AVAudioPlayer stops, the Now Playing info center automatically stops too, even tho I keep feeding it fresh nowPlayingInfo. Then when it hits another .audio item, it resumes correctly and shows current time, etc.
And here lies the question
how do I trick the MPNowPlayingInfoCenter into thinking that audio is being played while I "play" my .pause item?
I realise that it may still not be clear, what I am trying to achieve but I am happy to share more insight if needed. Thanks!
Some solutions I am currently thinking about:
A. Keeping 1s long empty audio track that would play on loop for as long as the pause is needed to play.
B. Creating programatically empty audio track with appropriate lenght and playing it instead of using Timer for keeping track of pause duration/progress and relying completely on AVAudioPlayer for both .audio and .pause items. Not sure this is possible though.
C. Maybe there is a way to tell the MPNowPlayingInfoCenter that the audio keeps playing without the need of using AVAudioPlayer but some API I am not familiar with?
AVAudioPlayer is probably the wrong tool here. You want AVAudioPlayerNode, which is slightly lower-level. Create an AVAudioEngine, and attach an AVAudioPlayerNode. You can then call scheduleFile(_:at:completionHandler:) to play the audio at the times you want.
Much of the Apple documentation on AVAudioEngine appears broken right this moment, but the links hopefully will be available again shortly in the links for Audio Engine Building Blocks. (If it stays down and you have trouble finding docs, leave a comment and I'll hunt down the WWDC videos and other tutorials on using AVAudioEngine. It's not particularly difficult for simple problems.)
If you know in advance how you want to compose these items (and it looks like you may), see also AVMutableComposition, which lets you glue together assets very efficiently, including adding empty segments of silence. See Media Composition and Editing for the various tools in that space.

SceneKit - Audio causes cxa_throw with a lag the first time I play a sound

I play back a sound like this ( this is inside a SCNNode subclass ):
let audioSource = SCNAudioSource(named: "coin.wav")
let audioPlayer = SCNAudioPlayer(source: audioSource)
self.addAudioPlayer(audioPlayer)
The first time this is called, I get a severe lag and an expection is thrown.
I notice the lag, when I disable the All_Expection_Breakpoint.
What can I do against this?
The C++ exception comes from AVAudioEngine that is used by the SceneKit audio layer. The AVAudio* framework uses C++ exceptions internally so if you have a breakpoint set in Xcode to break when C++ exceptions are thrown Xcode will break a lot in the AVAudio* code (mostly at init times). You can safely ignore these as they are caught by the framework before they reach your code anyway.
If you don't want the lag you can instantiate your audio source and load it at startup time:
let audioSource = SCNAudioSource(named: "coin.wav")
audioSource.load()
And then add the player when you need it later:
let audioPlayer = SCNAudioPlayer(source: audioSource)
self.addAudioPlayer(audioPlayer)
By the way, players are cached and recycled so you don't have to worry too much about memory being used for nothing.
Note also that the SCNAction uses exactly the same API than you do, so if you create an action with a sound that hasn't previously been loaded in memory with .load() you will also get a lag.
Hope this helps,
S.
This is really only for atmospheric, ambient and background environment sorts of sounds, to be looped and moved with a character or attached to the position of a waterfall or something like that.
It's not a performant, instant sound player for immediate sound effects requiring low latency.
For that you're better off using an SCNAction to play the audio as an Action when needed, or using something like Fmod that's designed for low latency sound playback.
I'm not sure how I know this.

Playing sound without lag in Swift

As many developers know, using AVAudioPlayer for playing sound in games can result in jerky animation/movement, because of a tiny delay each time a sound is played.
I used to overcome this in Objective-C, by using OpenAL through a wrapper class (also in Obj-C).
I now use Swift for all new projects, but I can't figure out how to use my wrapper class from Swift. I can import the class (through a bridging header), but when I need to create ALCdevice and ALCcontext objects in my Swift file, Xcode won't accept it.
Does anyone have or know of a working example of playing a sound using OpenAL from Swift? Or maybe sound without lag can be achieved in some other way in Swift?
I've ran to a delay-type problem once, I hope your problem is the same one I've encountered.
In my situation, I was using Sprite-Kit to play my sounds, using SKAction.playSoundFileNamed:. It would always lag half a second behind where I wanted it to play.
This is because it takes time to allocate memory for each SKAction call. To solve this, store the sound action in a variable so you can reuse the sound later without instantiating new objects. It saved me from the delay. This technique would probably work for AVAudioPlayer too.

sound design for spritekit with swift

I'm trying to figure out how to add some sound design elements to my game. For example.. I want an engine sound to change pitch or grow louder as my sprite moves faster. Obviously this is outside the scope of SKAction. I've tried AVAudioPlayer.. this works but it seems to be more suited towards playing music. Even running a short loop using AVAudioPlayer produces popping sounds between each loop.
How can I control things like pitch, volume, playback speed programmatically?
This seems useful.
http://kstenerud.github.io/ObjectAL-for-iPhone/index.html
but is there a swift version.. or can I bridge this over to swift somehow.
ObjectAL can be added as a Pod into a Swift project, i am using it right now in my own Swift projects.

AVAudioPlayer initialization & memory management

I am trying to figure out something -
Every time I use AVAudioPlayer I need to initialize a new AVAudioPlayer object.
I have 10 Sentence objects on a view and I wish to add a "PlaySentence" method to each Sentence object so when the user taps the Sentence the app will play a sound file.
I need this behavior on many views, so I thought adding the method to the object class so I can simply call -
[Sentence playSound];
Since AVAudioPlayer is any way initialized every time I wish to use it I can not see why this should be more expensive operation.
Am I right / is it a good approch for this need and why?
Thanks
Shani
So if I understand you right, you want your Sentence object have a playSound method which sets up an AVAudioPlayer and plays the sound.
You can definitely do it like that, but be aware that if you have a lot of Sentence objects then you will end up creating a lot of AVAudioPlayer objects. But you could stop the high water line being too high by releasing it at after the file has finished playing.
Another way you could do this is to have a method on Sentence to return the URL of the file to play and then just have a single AVAudioPlayer instance in your view controller where you want to play the sounds and set it up each time for the correct file. This would be my personal suggested way of doing it.
I think best solution for you is to use SimpleAudioEngine from Cocos2D library.
It's easy to use and integrates well. As I know, it uses OpenAL and AVAudioPlayer. It has easy interfaces to control background music, effects and works with mp3, wav, m4a and other formats and codecs.
Using it is very easy:
// it's better to preload all effects during app start so they will play immediately when you call "playEffect" method
[[SimpleAudioEngine sharedEngine] preloadEffect:#"sentence.m4a"];
...
// play effect where you need it
[[SimpleAudioEngine sharedEngine] playEffect:soundName];
You can find it here inside Cocos2D library. And you can find all useful information about it here.

Resources