LogoLogo
  • Home
  • Projects
  • About
  • Contact

Towards a Better Scratch

Devon O. · June 23, 2012 · Actionscript, Flash · 6 comments
13

Not sure what got me started on this project. Perhaps it was listening to too much Meat Beat Manifesto in the late 80’s. Or maybe it was my wife who got me to sit down and watch the Scratch documentary. Whatever the hell it was, I’ve been wanting to make a decent vinyl scratch app with Actionscript for quite some time now. Now this idea isn’t exactly new. Since the release of Flash Player 10, it’s been possible to manipulate the very byte data of sounds with the SampleDataEvent and many have toyed with this idea including Lee Brimelow, Grant Skinner, Andre Michelle,  (unfortunately you can’t actually see that one still), and this guy (sorry – the blog is in French and I couldn’t find an ‘about’ section to provide the actual name). All of these (including a similar one I did myself a few years back) pretty much sucked though – and I’ll tell you why…

The Problem

The trouble with digital audio is that its, well, too digitized. That is, when you write bytes to an audio stream, no matter how fast or slow or whether you write forwards or backwards, every single one of those bytes will be distinctly played. The result of that then isn’t so much a ‘sccrrrratcchh’ sound, but rather how you might imagine a fast moving tape would sound – sorta like a turkey all hopped up on meth. But back in the analog ‘real’ world, when you slide a vinyl disc underneath a needle, it won’t play every note distinctly – instead it kind of plays the sounds in between and mixes it all together. Fortunately, there’s a way of emulating the mixing of in between values digitally; with the fancy sounding word ‘interpolation’.

The Process

Now seeing as how I know as much about audio processing as I do quantum topology (and I don’t even know if that’s a real thing or if I just now made it up), I do what everyone does these days – I turned to Google to see if I could find any ready made examples I could just copy. The most comprehensive thing I could find was this C++ / Obj-C example from Jan Kalis. So I downloaded the source and made a half assed attempt at just a straight port to Actionscript. Not really knowing C++, Objective C, or audio processing really made that a rather difficult task and I soon just gave up and went back to googling. While I couldn’t find any better source code, every blog post, forum thread and stack overflow question I came across all mentioned that magic word: ‘interpolation’ – quite often preceded by the word ‘hermite’.  Actually, one other place where source code was available was in the Mixxx project (incidentally, Mixxx is a great open sourced DJ software project – I definitely recommend downloading it and playing around if you’re in to that sort of thing). So after digging around through the Mixxx source, I discovered that they were, indeed, using hermite interpolation to create their scratch sound. That then seemed to be the key. Still I was stuck though. I had no idea how to implement hermite interpolation with sound in Actionscript. So back to Google I went. And that’s when I came across this great blog post from Alex Nino. I downloaded the example files and immediately learned two things – how to use interpolation with sound and the very nifty little trick of extracting float samples from a sound’s byte array and storing them in a vector for easier access.

So I turned back to my own project and tried those two tricks and interpolating between a current position and target position based on mouse movement. I compiled expecting to hear amazing things and… nada. I still had my hopped up turkey. I searched around for other interpolation methods and tried 3 or 4 others. All without any change. At this point I cussed and fumed and kicked the floor and swore to give up on the project.

After that, I went back to the Jan Kalis source code. Now that I knew a bit more about storing and accessing sound float values and interpolating those values with Actionscript, all that C++ suddenly made a lot more sense. So with a bit of tweaking, fiddling, tinkering, and fine tuning that cpp into as, I finally came up with the below..

Get Adobe Flash player

It ain’t perfect, but it’s the best I’ve heard in Actionscript yet. And if you’re interested in the source, you check it all out over on Wonderfl.

wiki wiki…

  Facebook   Pinterest   Twitter   Google+
  • And Never Again, I’ll Go Sailin’
    September 16, 2008 · 6 comments
    1921
    3
    Read more
  • Unity3D Endless Runner Part I – Curved Worlds
    July 17, 2016 · 8 comments
    A couple months back I got an overpowering hankerin' to create an endless
    18348
    61
    Read more
  • Billboard Transition Effect in Flash Player 10
    August 03, 2008 · 9 comments
    2578
    4
    Read more
6 Comments:
  1. Last week I have also worked a bit with sound and byteArray for a client project.
    I see you have made a great piece of work here!
    Keep up the good work!!

    vamapaull · June 24, 2012
  2. Brilliant work!!

    If you scratch very slowly it’s not very recognisable as the actual mp3 (i.e. if you try and drag as if to play the song at its original speed), but I guess that’s down to what is actually going on in the implementation. That said it sounds awesome, a job well done sir. I wrote a little 3d turntable app and had scratching in that but wasn’t happy with the result… might have to try and plug in what you have done and see how that sounds!!!

    Any idea on the performance of the method you adopted… from my dabblings with sound, some techniques can be quite taxing on the ol cpu.

    i.e. can the re-sampling be performed in sub 3ms on a decent machine? Just curious (and lazy as I could just try and profile it myself but super busy at the mo)

    once again congratulates on by far the best sounding attempt at this I have heard to date :)

    ben w · June 25, 2012
  3. Thank you Paul!

    @Ben, Thank you for the comments. You’re right about the slow movement, but it was a bit of tradeoff. There’s still a lot of work that could be done on it. To be honest, I put the source on wonderfl in the hopes that someone who knew more about audio than myself would fork it and make it better. Not really sure about the performance, but it was built on a rather oldish laptop and worked fine there. If you give it a go on your 3D turntables, let me know – I wouldn’t mind checking it out, even if it’s just a video.

    And, completely unrelated, but that was some great work on your AGAL Helper as well.

    Devon O. · June 25, 2012
  4. Well as soon as I am any less than stupidly busy with unrelated work, I had planned to upload a demo, so if (hopefully when) I get round to it I’ll be sure to let you know and send you a link.

    ben w · June 25, 2012
  5. An awesome side-effect: If you click on the waveform and just keep it still, you get an awesome deep rumble. It sounds a bit like the engine room on the Enterprise.

    Lawrie · June 28, 2012
  6. Wasn’t going to mention it, but that’s a feature – not a bug. Honestly.

    Devon O. · June 28, 2012

Leave a Comment! Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Devon O. Wolfgang

AIR | Unity3D | AR/VR

Unity Certified Developer

Technical Reviewer of “The Essential Guide to Flash CS4 AIR Development” and “Starling Game Development Essentials”

Reviewer of “The Starling Handbook”

Unity Engineer at Touch Press.

Categories
  • Actionscript (95)
  • AIR (16)
  • Flash (99)
  • Games (7)
  • Liberty (13)
  • Life (53)
  • Shaders (20)
  • Unity3D (21)
Recent Comments
  • Devon O. on Unity Ripple or Shock Wave Effect
  • Feral_Pug on Unity Ripple or Shock Wave Effect
  • bavvireal on Unity3D Endless Runner Part I – Curved Worlds
  • Danielius Vargonas on Custom Post Processing with the LWRP
  • Luca G on Unity Ripple or Shock Wave Effect
Archives
  • December 2020 (1)
  • December 2019 (1)
  • September 2019 (1)
  • February 2019 (2)
  • December 2018 (1)
  • July 2018 (1)
  • June 2018 (1)
  • May 2018 (2)
  • January 2018 (1)
  • December 2017 (2)
  • October 2017 (1)
  • September 2017 (2)
  • January 2017 (1)
  • July 2016 (1)
  • December 2015 (2)
  • March 2015 (1)
  • September 2014 (1)
  • January 2014 (1)
  • August 2013 (1)
  • July 2013 (1)
  • May 2013 (1)
  • March 2013 (2)
  • December 2012 (1)
  • November 2012 (1)
  • September 2012 (3)
  • June 2012 (2)
  • May 2012 (1)
  • April 2012 (1)
  • December 2011 (2)
  • October 2011 (3)
  • September 2011 (1)
  • August 2011 (1)
  • July 2011 (1)
  • May 2011 (2)
  • April 2011 (2)
  • March 2011 (1)
  • February 2011 (1)
  • January 2011 (2)
  • December 2010 (3)
  • October 2010 (5)
  • September 2010 (1)
  • July 2010 (2)
  • May 2010 (5)
  • April 2010 (2)
  • March 2010 (7)
  • February 2010 (5)
  • January 2010 (5)
  • December 2009 (3)
  • November 2009 (1)
  • October 2009 (5)
  • September 2009 (5)
  • August 2009 (1)
  • July 2009 (1)
  • June 2009 (2)
  • May 2009 (6)
  • April 2009 (4)
  • March 2009 (2)
  • February 2009 (4)
  • January 2009 (1)
  • December 2008 (5)
  • November 2008 (2)
  • September 2008 (1)
  • August 2008 (6)
  • July 2008 (6)
  • June 2008 (9)
  • May 2008 (4)
  • April 2008 (3)
  • March 2008 (4)
  • February 2008 (9)
  • January 2008 (7)
  • December 2007 (6)
Copyright © 2017 Devon O. Wolfgang