Pitch MP3

July 2nd, 2009

Pitching MP3 (Not PitchShift!) is possible since Flash10s new Sound API. Today I saw Lee Brimelow's post about doing so, providing some source code. Cause we spend a lot of time to keep things running more smoothly in the AudioTool, I created another version which has some advantages.

  • No objects are created in runtime (memory usage)
  • The SampleDataEvent is receiving a static blocksize (steady latency)
  • Linear interpolation (sound quality)
  • Speed can go down to zero

{source code}

Get Adobe Flash player

(no preloader - music by basic channel) - Thanks Lee for pointing me!

Filed under: actionscript

53 Responses to “Pitch MP3”

  1. Jop Wielens Says:
    July 2nd, 2009 at 12:41 pm

    Seriously cool! Can’t believe the source code is so small and easy to follow.
    Already looking forward to your FOTB talk. Hope they put you in the room with the decent soundsystem ;-)

  2. Patrick Says:
    July 2nd, 2009 at 12:46 pm

    Awesome! Love your work, thanks for this example!

  3. Swingpants Says:
    July 2nd, 2009 at 1:13 pm

    Very nice use of Basic Channel…
    Been meaning to do some audio work, something like pitching is a great place to start.

  4. Tobias Hinz Says:
    July 2nd, 2009 at 1:18 pm

    wow nett – basic channel rockt und das script auch.

    wann programmierst du traktor dj studio in actionscript nach? ;)

    grüße aus berlin

  5. Thomas Junk Says:
    July 2nd, 2009 at 1:38 pm

    Aber es pitched doch gar nicht, es speeded … ;-)
    Geht das auch mit pitchshift und harmonic correction?

  6. dVyper Says:
    July 2nd, 2009 at 1:49 pm

    Nicely done! Now we just need to figure out how to get the music pitch changed but with the music playing at a constant speed …

  7. Philipp Says:
    July 2nd, 2009 at 1:54 pm

    bekomm leider den folgenden fehler?!

    TypeError: Error #2007: Parameter type darf nicht ‘null’ sein.
    at flash.events::EventDispatcher/addEventListener()
    at components::MP3Pitch()[/Users/aM/Documents/workspace/audiotool/team/andre.michelle/fdt/session/Audio2009/src/components/MP3Pitch.as:36]
    at ExampleMP3Pitch()[/Users/aM/Documents/workspace/audiotool/team/andre.michelle/fdt/session/Audio2009/src/ExampleMP3Pitch.as:29]

  8. Andre Michelle Says:
    July 2nd, 2009 at 3:17 pm

    @Philipp:
    Neusten Flashplayer installiert? Klingt als hättest du noch den Betaplayer.

  9. Kodiak Says:
    July 2nd, 2009 at 3:17 pm

    Always a fan :)

    I did something quite similar there:
    http://guillaume.boit.free.fr/flash/scratch/bin/
    (long preloader, just wait)

    Sad thing it’s not very reactive.

  10. Radley Marx » Blog Archive » Pitch Shift in Flash (Digital DJs may wet themselves now) Says:
    July 2nd, 2009 at 4:41 pm

    [...] links to article & player: (blog.andre-michelle.com) Pitching MP3 (Not PitchShift!) is possible since Flash10s new Sound API. Today I saw Lee [...]

  11. Pitch-shifting MP3s in Flash | vj.tv Says:
    July 2nd, 2009 at 5:03 pm

    [...] links to article & player: (blog.andre-michelle.com) Pitching MP3 (Not PitchShift!) is possible since Flash10s new Sound API. Today I saw Lee [...]

  12. Alfredo Chu Says:
    July 2nd, 2009 at 6:59 pm

    Great stuff… Keep up the great work.

    @Kodiak nice turntable.

  13. Pitch MP3 | Adobe Tutorials Says:
    July 2nd, 2009 at 7:49 pm

    [...] Read the rest here: Pitch MP3 [...]

  14. The Flash Blog » Adjusting audio pitch in Flash Player 10 Says:
    July 2nd, 2009 at 8:22 pm

    [...] code to adjust the pitch of an external sound. I was secretly hoping he would. Check it out at http://blog.andre-michelle.com/2009/pitch-mp3/. Thanks [...]

  15. vidme Says:
    July 2nd, 2009 at 10:04 pm

    AwESoMe …
    could this code (modified) used for video?
    :-)

  16. Lee Brimelow Says:
    July 3rd, 2009 at 6:58 am

    That sounds amazing Andre!

    @vidme unfortunately there is no current way to get to the bytes of audio from a Flash video file.

  17. E.G. Says:
    July 3rd, 2009 at 9:54 am

    @dVyper
    you just estimate the pitch and slice the sample accordingly and then overlap+add

    @andre
    not sure if it matters in AS3, but you could save a multiplication in
    ( 1.0 – alpha ) * l0 + alpha * l1 )
    ->
    l0 – alpha* l0 + alpha * l1
    ->
    l0+alpha*(l1-l0)

    but maybe the compiler does already these kind of optimizations.. do you happen to know if this is the case?

  18. makc Says:
    July 3rd, 2009 at 12:02 pm

    Dont know if it’s the same thing but check it out http://wonderfl.kayac.com/code/d1eb34ea56d0b2671a0b85ce78b75896dba8573f (2009/05/20)

  19. leef Says:
    July 4th, 2009 at 12:20 am

    omg, I love it! Nice add adobe!!! = ]

  20. Andre Michelle Says:
    July 4th, 2009 at 1:35 pm

    @E.G Thanks for sharing. I have updated the source code with your optimization.

  21. Hebiflux » De l’UGC musical avec LG Says:
    July 8th, 2009 at 10:01 am

    [...] au vu des expérimentations d’Andre Michelle avec son pitch mp3, on devrait avoir pas mal de créations originales avec les effets d’accélération ou de [...]

  22. Adrien W. Says:
    July 8th, 2009 at 10:20 pm

    Andre you rock! Your implentation of mp3 speed shifting is very elegant.

    I’m wondering, is it possible to use sound.extract() and then manipulate the raw sound data without waiting that the song is completely loaded ?

  23. elearning Says:
    July 11th, 2009 at 9:45 pm

    Good work !

    Keep up great staff!

  24. Doug Says:
    July 13th, 2009 at 6:38 am

    > you just estimate the pitch and slice the sample accordingly and then overlap+add

    Could you perhaps elaborate on how to time shift without shifting pitch? Or better yet, update this example so that pitch is not effected? :)

    @Adrien W. – yes, it seems you can start extracting before the file completely loads.

  25. jazzairwaves Says:
    July 14th, 2009 at 1:56 am

    In addition to the Speed control, would it be possible to create a second slider to control the pitch? I suppoe it would be similar to what E.G. suggested, but I’m not sure how to code the second slider. Has anyone done both rate and pitch sliders using the Sound API and SampleDataEvent?

  26. Ryan Berdeen Says:
    July 15th, 2009 at 10:44 pm

    @Doug, @jazzairwaves, @dVyper

    I’ve started porting a C++ library (SoundTouch) that does the overlap+add for time-stretching/pitch shifting. It’s pretty rough, but it works and is promising:

    Here’s a demo http://static.ryanberdeen.com/projects/soundtouch-as3/demo/player/stretch.swf

    You can adjust the tempo by dragging the slider. It sounds OK when the tempo is increased. Decreasing the tempo could use some work.

    The source is here: http://github.com/also/soundtouch-as3

    Ryan

  27. Doug Says:
    July 16th, 2009 at 2:53 am

    @Ryan – that’s exceedingly cool! And even without decreasing tempo, it’s a lot further along than my attempted port of this FFT based routine:

    http://downloads.dspdimension.com/smbPitchShift.cpp

    I seem to be having trouble with very basic AS Number issues – the output has some relation to the input, but there are many artifacts… Will post if I figure it out. Please update if you get lower tempos working. Wouldn’t it be a matter of overlapping copies of each frame?

  28. sth Says:
    July 17th, 2009 at 10:43 pm

    Hi, how to pitch a mp3 loop ?

  29. sth Says:
    July 19th, 2009 at 5:27 am

    Hi, so I find the way, but it’s not perfect

    http://stealth35.free.fr/flash/Looper.as

  30. Twitted by joe_schorn Says:
    July 19th, 2009 at 11:06 am

    [...] This post was Twitted by joe_schorn [...]

  31. Pitch MP3; MP3 in toonhoogte en afspeelpositie variëren | Marco Raaphorst Says:
    July 19th, 2009 at 4:19 pm

    [...] Klikken maar! (en ff geduld hebben met het laden van het geluid) [...]

  32. Adrien W. Says:
    July 19th, 2009 at 8:19 pm

    @Ryan
    Major respect, that’s the most impressive thing I’ve seen in audio with AS3 !
    Looks very promising indeed :)

  33. bechar Says:
    July 21st, 2009 at 10:47 am

    Hi,

    Anyone know how can i put the buffer when i play dynamic sound(it’s generate from raw sound(mp3 file))

    Thanks
    Bechar

  34. Sound Pitch Says:
    July 22nd, 2009 at 10:59 pm

    [...] i was talking with Caleb (the sound expert here at Grupow) a few days ago about how the Pitch works behind the scenes i saw a great usage of the Pitch effect in Braid Game and i wanted to give a try in the new Sound API en Flash …………. and then Andre Michelle came with this!!! [...]

  35. atom Says:
    July 23rd, 2009 at 11:23 am

    Andre, yet again you rock \m/ \m/

  36. ian pretorius Says:
    July 26th, 2009 at 6:12 am

    it keeps getting better , thanks for this

  37. Paras Says:
    July 27th, 2009 at 5:58 pm

    hey Andre,

    great stuff as always!

    any chance you could point me in the direction to do BPM (Beats Per Minute) analysis for a batch of MP3 files?

    FP 10 is fine too.

    Thanks in advance

  38. atom Says:
    July 27th, 2009 at 7:21 pm

    Just in case anyone is looking to extend this example to do note based pitch shifting, this formula might be handy:

    _playbackSpeed = Math.exp((Math.log(2)* SEMITONE_SHIFT)/12);

    Hope that helps,

    atom

  39. jazzairwaves Says:
    August 3rd, 2009 at 10:32 pm

    @Ryan – Superb example of the type of audio control we’ve been struggling to create in Flash for a long time. Thank you for providing the source! Excellent work!

  40. marcin p Says:
    August 12th, 2009 at 12:03 am

    those chain reaction / bacis channel classics still sound good after all these years :)

  41. Having fun with Face Tracking and MP3 Pitch control | Jop Wielens Says:
    September 11th, 2009 at 10:39 am

    [...] this year we saw the Flash Face Tracking and 2 months ago Andre Michelle showed how easy it was to control the pitch of a sound in Flash 10. I love all that [...]

  42. filmy online Says:
    September 13th, 2009 at 5:13 pm

    Hi,

    Anyone know how can i put the buffer when i play dynamic sound(it’s generate from raw sound(mp3 file))

    Thanks
    Bechar

  43. leef Says:
    September 29th, 2009 at 3:47 am

    Maybe this will help someone else, the _position variable is the Byte position, not millisecond = \ sometimes I spent way to much time not noticing that

  44. Namake Says:
    November 12th, 2009 at 5:56 pm

    Hi Andre,

    your work is awesome!
    How to pitch a loop mp3?

  45. michael Says:
    December 15th, 2009 at 10:47 pm

    This is also pretty cool, as the tempo stays constant when you change pitch: http://iq12.com/blog/2009/08/25/real-time-pitch-shifting/

  46. Olof Says:
    December 18th, 2009 at 3:01 pm

    Really great work!

    Im not good at this soundstuff. Is there a way to use this on soundfiles with lower samplerate or any samplerate?

    //Olof

  47. Olof Says:
    December 19th, 2009 at 3:23 pm

    @leef you can get the percent played like this: var percentPlayed:Number = (_position/(_mp3.length*44.1));

  48. Michael Says:
    December 30th, 2009 at 7:38 pm

    Great work! Thanks for sharing!

  49. Roland Says:
    December 31st, 2009 at 5:48 pm

    if anybody else would like to play the sound from a certain start position you have to set the position to something like this:

    _position = startTime * 44.1;

    note that the startTime will be the time the mp3 should start playing in milliseconds.

    hope that helps.

  50. Jonas Says:
    January 3rd, 2010 at 6:08 pm

    Awesome!
    Does anyone know how to play the mp3 in reverse smoothly? Right now I have changed the startposition:

    _position = startTime*44.1;

    and the sound position:

    _position -= scaledBlockSize;

    But it doesn’t play smoothly…

    Thanks

  51. Matthew Says:
    January 5th, 2010 at 11:10 pm

    This is a really amazing thing you have done here, thank you…

  52. MP3 Wave Display 2 Says:
    January 17th, 2010 at 9:18 pm

    [...] speed and maintain acceptable sound quality. I took the code for pitching a sample from this MP3 Pitch example by Andre Michelle, which builds on Lee Brimelow’s post Adjusting audio pitch in Flash Player [...]

  53. mit popforge wave in anderer SamplingRate einbinden - Flashforum Says:
    February 7th, 2010 at 2:23 pm

    [...] meine, dass der Wavplayer in Popforge jede Geschwindigkeit abspielen kann. Ansonsten hilft auch dieser Code oder dieser. __________________ aM blog | hobnox | laboratory | sources | processing Adobe, [...]

Leave a Reply