Slow motion effect for sound

Hello,

I’m very new to this engine. Actually I’ve interested in it because I need to implement one simple(as I think) thing – it’s slowing down music and sounds when pause menu occurs and accelerate up after it hides.

Something like here – https://vimeo.com/174327353

Is there a simple way achieve this? I tried just to set using default cocos2d-iphone engine pitch linearly, but it work very bad..

How this effect can be done with The Amazing Audio Engine?

Comments

  • You can utilize the varispeed filter within The Amazing Audio Engine via the AEVarispeedModule. You can even see how it's implemented in the excellent sample app project that's provided in TAAE 2 source on GitHub:

    https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine2

    You can see it in action in the sample app preview video at 1:52 seconds:

    https://www.youtube.com/watch?v=q9dcApliV-Y#t=1m52s

    The varispeed is slowing down the speed with the left hand, while the right hand is manipulating the bandpass filter. Here's the full video, if you're interested: https://www.youtube.com/watch?v=q9dcApliV-Y

    This is a code snippet from TAAE 2's AEVarispeedModule.h file:

    //! documented range is from 0.25 to 4.0, but empirical testing shows it to be 0.25 to 2.0. Default is 1.0.
    @property (nonatomic) double playbackRate;
    
    //! range is from -2400 to 2400. Default is 0.0.
    @property (nonatomic) double playbackCents;
    

    Normal playback rate is 1.0, which is its default. In your case, you may just want to change the playbackRate between 0.25-0.5 in order to slow it down for the desired effect. In order to hear the slow down in real-time, you should implement an NSTimer, so that you can hear the change happen over time, during the duration of the animation speed change. You can find this in the sample app in the following code:

    Line 188:

    @IBAction func speedSliderDrag() {
            if speedRestoreTimer != nil {
                // Get rid of prior timer
                speedRestoreTimer?.invalidate()
                speedRestoreTimer = nil
            }
            
            // Set the playback rate based on the slider position
            updatePlaybackRate(Double(speedSlider!.value))
        }
        
        @IBAction func speedSliderEnd() {
            // Finished interacting with slider - set a timer to gradually bring the speed back to normal
            speedRestoreTimer = NSTimer.scheduledTimerWithTimeInterval(SpeedSliderRestoreInterval, target: self,
                                                                       selector: #selector(speedSliderRestoreTimeout), userInfo: nil, repeats: true);
        }
    

    Line: 470:

    // Transition speed back to normal
        @objc private func speedSliderRestoreTimeout() {
            if let speedSlider = speedSlider {
                if speedSlider.value > 1.0 {
                    speedSlider.value = max(1.0, speedSlider.value - Float(SpeedSliderRestoreRate))
                } else {
                    speedSlider.value = min(1.0, speedSlider.value + Float(SpeedSliderRestoreRate))
                }
                
                updatePlaybackRate(Double(speedSlider.value))
                
                if speedSlider.value == 1.0 {
                    speedRestoreTimer?.invalidate()
                    speedRestoreTimer = nil
                }
            }
        }
        
        @objc private func updatePlaySlider() {
            playSlider.value = Float(audio!.recordingPlaybackPosition)
        }
    
  • edited July 2016

    Hello,

    thanks for prompt reply. I'm using Objective C only and Cocos2D, so Swift code example for me is hard to read for now..
    Is this same possible with v1 engine?
    Also, I'm just looking for simple demo project, that just demonstrates changing of pitch for sounds. Can you please provide such example in Objective C?

  • edited July 2016

    Also, I'm trying to add TheAmazingAudioEngine2 to a simple even UIKit project, following instructions http://theamazingaudioengine.com/doc/_getting-_started.html#Setup and when I try to import it : #import <TheAmazingAudioEngine/TheAmazingAudioEngine.h>
    I got an error:
    'TheAmazingAudioEngine/AEBufferStack.h' file not found

    Here is my test project:

  • edited July 2016

    Pls disregard previous message. Added "$(BUILT_PRODUCTS_DIR)" to header seach path. Pls include this into documentation.

    So, one question left for me: can I use old and solid version(that has some getting started guide) of engine and apply pitch?

    Or I should use new one? Could someone please show simple Objective C example of doing so for v2?
    I'm looking for just loading audio file and changing it pitch. This will be really helpful to me to integrate into my cocos2d-spritebuilder game code.

  • edited July 2016

    Hello,

    I'm using this code for loading and playing audio. All seems to working fine

    `
    -(instancetype) init {

    self = [super init];
    
    
    AERenderer *renderer = [AERenderer new];
    AERenderer *subrenderer = [AERenderer new];
    
    NSString* fullPath = [[CCFileUtils sharedFileUtils] fullPathForFilenameIgnoringResolutions:@"Sounds/game_1.wav"];
    NSURL *url = [NSURL URLWithString:fullPath];
    AEAudioFilePlayerModule * gameMusic = [[AEAudioFilePlayerModule alloc] initWithRenderer:renderer URL:url error:NULL];
    
    NSMutableArray * players = [NSMutableArray array];
    [players addObject:gameMusic];
    
    AEMixerModule * mixer = [[AEMixerModule alloc] initWithRenderer:subrenderer];
    mixer.modules = players;
    
    AEVarispeedModule *varispeed = [[AEVarispeedModule alloc] initWithRenderer:renderer subrenderer:subrenderer];
    //documented range is from 0.25 to 4.0, but empircal testing shows it to be 0.25 to 2.0. Default is 1.0.
    varispeed.playbackRate = 0.25;
    //range is from -2400 to 2400. Default is 0.0.
    varispeed.playbackCents = 0.0;
    self.varispeed = varispeed;
    
    subrenderer.block = ^(const AERenderContext * _Nonnull context) {
        AEModuleProcess(mixer, context);
        AERenderContextOutput(context, 1);
    };
    
    renderer.block = ^(const AERenderContext * _Nonnull context) {
        AEModuleProcess(varispeed, context);
        AERenderContextOutput(context, 1);
    };
    
    gameMusic.loop = YES;
    [gameMusic playAtTime:AETimeStampNone];
    
    self.output = [[AEAudioUnitOutput alloc] initWithRenderer:renderer];
    
    return self;
    

    }

    • (BOOL)start:(NSError * _Nullable * _Nullable)error {
      return [self.output start:error];
      }
      `

    However, changing varispeed.playbackRate does very bad sound while it's slowing down
    I'm trying to do something like here – https://vimeo.com/174327353
    But how can I achieve such effect? Currently I'm just decreasing it's value:
    .playbackRate -= 0.04; each 1/60 times per frame in update method.

  • Try modifying the varispeed.playbackCents, instead of varispeed.playbackRate. 0 to -2400 sounds pretty good. I would use an NSTimer to affect the change over time.

    FYI, I'm working on an Objective-C sample project for you that will demo the following, using TAAE 1:

    1. Slow down and speed up an audio file, utilizing a pause/play UIButton, an NSTimer, and the Varispeed filter.
    2. The ability to modify the pitch, pan, and volume of an audio file via AUSampler.
    3. Apply filter effects, such as delay, reverb, distortion and EQ.to specific audio channels or the global output mix.

    I'll post it on GitHub when it's ready. it should be soon.

    Take care,
    Mark

  • Hello Mark,

    I've already tried to change .playbackCents also.
    Here is small part of my music game theme : https://www.dropbox.com/s/0kfq0u1nscuepop/game_2.wav?dl=0
    When I apply playbackCents it's sounds a little bit better but it's far far away from what I'm looking for, like in that vimeo video.

    Example from TAAE 1 is interesting for me, especially with all features you describe. Because for my game I need exactly this.

  • edited July 2016

    @klymenko.volodymyr.m,

    I checked out your game theme. Of course, your music is going to sound different when slowed down, compared to a buzzing saw with reverb. What exactly doesn't sound right to you when you implement the varispeed playbackCents solution? Are you starting and stopping the music's playback with an NSTimer? Is there any chance you could please post some code or at least an audio example of your varispeed result?

    I also noticed in the Vimeo example, that the Badlands menu includes its own sound effect when it swipes up and down, as soon as the gameplay is paused or restarted.

  • edited July 2016

    @markjeschke said:
    Is there any chance you could please post some code or at least an audio example of your varispeed result?

    Easily. I'm using cocos2d, but it has similar CCTimer, so yes, I'm using it. It ticks fixed 1/60.

    upd.: when I created simple example with clean project, I've found some bug in my actual game code - it wasn't set correctly that value(very fast decrease) so now I have a good working example(not ideally like in Badland) and it sounds like in vimeo video. Yes, with my game music, it will sounds really different and now, I realised, that it's really depends on the music and slowing down my sample sound not really nice..
    Maybe, to make it better, I should apply high pass filter after 0.1ms beginning of decreasing playbackCents value. Because I think that bass sounds, like in my music because sounds worse at low speed.. Need to find how to do that...

    Thanks for your help. However, I'm still very interesting in your example with TAAE 1.
    For my game, I would need these actions: play, pause, stop, mute, handle interrupt(for example when game minimized sound should correctly paused) and pan.

  • @klymenko.volodymyr.m,

    I'm sorry for the delay with this. But, here's the Xcode Objective-C example that I promised you, that demos how to pause and play an audio file with a slow-down torque effect with the varispeed filter. In addition, it includes panning and volume control:

    https://github.com/markjeschke/TAAE1-Torque

    Take care,
    Mark

  • Mark you're awesome! Thank you!

  • You're very welcome. It was a fun exercise to implement. Please let me know if you have further questions.

Sign In or Register to comment.