Advertisement
Guest User

0x40 Hues of Megumi

a guest
Aug 31st, 2013
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 0x40 Hues of Megumi
  2.  
  3. Audio looping and animation synchronization have always been difficult to blend in Flash. After years of study, trial and error, and experimentation, I've discovered a number of ways to make it work. Using streaming audio and the timeline to control animation is good, but in long animations, I have found it can drift out of sync. Streaming audio also does not lend itself to looping, as gaps are almost certain to be introduced because of the amount of time each frame uses in the timeline. Therefore, actionscript is the way to get a better control of these issues. Note that this example uses ActionScript 2.0, and does not contain all of the code from the file, just the important stuff.
  4.  
  5. In order to solve the problem of synchronizing music to animation with looped audio, a few things needed to be established:
  6.  
  7. 1. Isolate a loop of music from a song, and save as a .wav file.
  8.  
  9. It's important to save as a .wav file, NOT an .mp3 file. Why? It seems that .mp3 files tend to tack on a miniscule gap of silence to the file when saved, whereas a .wav file does not change the waveform at all. This makes for a seemless loop of sound.
  10.  
  11. 2. Map the drum beats to a simple string of characters, so that the animation will be affected by those beats.
  12.  
  13. You're just going to have to have a sense of rhythm and a keen ear to do this. The beat pattern can actually be seen scrolling at the bottom of the animation, but this is the actionscript used:
  14.  
  15. // START CODE BLOCK
  16. rhythm = "";
  17. rhythm += "x..xo...x...o...";
  18. rhythm += "x..xo...x...o...";
  19. rhythm += "x..xo...x...o...";
  20. rhythm += "x..xo...x...oxoo";
  21. rhythm += "x..xo...x...o...";
  22. rhythm += "x..xo...x...o...";
  23. rhythm += "x..xo...x...o...";
  24. rhythm += "x...o...x...oooo";
  25. // END CODE BLOCK
  26.  
  27. Note that the 'x' and 'o' are different drum types, and each do something slightly different in the animation.
  28.  
  29. 3. In the main loop, check what beat the music is on, and perform an action if there is a string match.
  30.  
  31. You must first initialize a few variable before the loop:
  32.  
  33. // START CODE BLOCK
  34. rhy = length(rhythm);
  35. blen = song.duration/rhy;
  36. beat = nextup = 0;
  37. // END CODE BLOCK
  38.  
  39. The 'rhy' variable is equal to 128 in this particular song, which is the number of beats in the audio piece.
  40. The 'blen' variable is the length of each beat in milliseconds.
  41. The 'beat' variable is which beat is currently being played.
  42. The 'nextup' variable is the millisecond value that the next beat falls at.
  43.  
  44. // START CODE BLOCK
  45. if(song.position>nextup&&(song.position-nextup)<1500) {
  46. if(rhythm.charAt(beat)=="x") { yblur = 64; }
  47. if(rhythm.charAt(beat)=="o") { xblur = 64; }
  48. if(rhythm.charAt(beat)<>".") { changeit(); }
  49. beat = (beat+1)%rhy;
  50. nextup = int(beat*blen);
  51. }
  52. // END CODE BLOCK
  53.  
  54. In the loop, we are always checking the song position versus the next beat value in milliseconds. If the song position is beyond the 'nextup' value, AND the difference between the two is less than 1.5 seconds, then the next beat has passed in the audio, and we need to check what character is found in the rhythm string. Why the 1.5 second check? If the audio is nearly at the end and ready to loop, the 'nextup' value is going to be zero, which will always trigger the first part of the conditional. The 1.5 second check ensures the beat and nextup values are close in value.
  55.  
  56. If the conditional is triggered, we check the character at the position 'beat' in the 'rhythm' string. If it's an 'x', we are going to start a y-directional blur on the picture. If it's an 'o', we'll do an x-directional blur. If it's either an 'x' or an 'o' (any drum, i.e. not a '.'), we'll run a color changing function on the picture.
  57.  
  58. The beat number is incremented (wrapping to zero if we pass the rhythm string length), and the 'nextup' variable is calculated. We multiply the current beat number by the beat length calculated earlier. We want to do the 'int' here so we don't get rounding errors.
  59.  
  60. Thanks to Michael Stone, original author of 50 Hues of Megumi, for the inspiration.
  61.  
  62. - Anonymous D!Noy5sI6il.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement