Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. package
  2. {
  3. import flash.display.*;
  4. import flash.events.*;
  5. import flash.text.*;
  6. import flash.net.*;
  7. import flash.media.*;
  8. import flash.utils.*;
  9. import fl.controls.Button;
  10.  
  11. import org.as3wavsound.WavSound;
  12. import org.bytearray.micrecorder.MicRecorder;
  13. import org.bytearray.micrecorder.encoder.WaveEncoder;
  14. import org.bytearray.micrecorder.events.RecordingEvent;
  15.  
  16. import com.noteflight.standingwave3.elements.*;
  17. import com.noteflight.standingwave3.filters.*;
  18. import com.noteflight.standingwave3.formats.*;
  19. import com.noteflight.standingwave3.generators.*;
  20. import com.noteflight.standingwave3.modulation.*;
  21. import com.noteflight.standingwave3.output.*;
  22. import com.noteflight.standingwave3.performance.*;
  23. import com.noteflight.standingwave3.sources.*;
  24. import com.noteflight.standingwave3.utils.*;
  25.  
  26. import com.greensock.*;
  27.  
  28. import fr.kikko.lab.ShineMP3Encoder;
  29.  
  30. public class VOCWordToYourMp3 extends MovieClip {
  31. // mic vars
  32. public var recorder:MicRecorder = new MicRecorder(new WaveEncoder());
  33. private var recording:Boolean = false;
  34. // SW3 vars
  35. public var player:AudioPlayer = new AudioPlayer()
  36. //public var sequence:ListPerformance = new ListPerformance()
  37. // mp3 vars
  38. private var mp3Encoder:ShineMP3Encoder;
  39. private var myWavData:ByteArray = new ByteArray()
  40. private var myWavFile:ByteArray = new ByteArray()
  41. // UI vars
  42. public var wavbtn:Button
  43. // Sine vars
  44. private var speedX:Number = 1;
  45. private var speedAngle:Number = 0.3;
  46. private var amplitude:Number = 45;
  47. private var angle:Number = 0;
  48. private var xpos:Number = 0;
  49. private var ypos:Number = 0;
  50. private var centerY:Number = 350;
  51.  
  52. public var jtlogo:MovieClip;
  53.  
  54. public function VOCWordToYourMp3()
  55. {
  56. btnrec.addEventListener(MouseEvent.CLICK, onBtnClick)
  57. btnrec.addEventListener(MouseEvent.ROLL_OVER, onBtnRoll)
  58. btnrec.addEventListener(MouseEvent.ROLL_OUT, onBtnRoll)
  59. btnrec.buttonMode = true;
  60. statustxt.text = "click the shiny button to begin recording and then allow Flash access to your microphone";
  61.  
  62. recorder.addEventListener(RecordingEvent.RECORDING, onRecording)
  63. recorder.addEventListener(Event.COMPLETE, onRecordComplete)
  64.  
  65. player.addEventListener(Event.COMPLETE, onPlayComplete)
  66. wavbtn.addEventListener(MouseEvent.CLICK, onWavClick)
  67.  
  68. soundMeter.scaleX = 0
  69. //initSineDrawer()
  70. }
  71.  
  72. public function onBtnClick(e:MouseEvent) { startRecording() }
  73. public function onBtnRoll(e:MouseEvent) { }
  74.  
  75. public function initSineDrawer()
  76. {
  77. // Sine Drawing
  78. graphics.lineStyle(1.5, 0x000000);
  79. graphics.moveTo(0, 355);
  80. }
  81.  
  82. public function startRecording()
  83. {
  84. if (!recording)
  85. {
  86. TweenMax.to(btnrec, .3, {glowFilter:{color:0xFF0000, alpha:1, blurX:50, blurY:50}} )
  87.  
  88. recorder.record()
  89.  
  90. } else if (recording) {
  91. recorder.stop()
  92. recording = false
  93. TweenMax.to(btnrec, .3, {glowFilter:{color:0xFF0000, alpha:0, blurX:10, blurY:10}} )
  94. }
  95. }
  96. public function onRecording(e:RecordingEvent)
  97. {
  98. statustxt.text = "make some noise!"
  99. var al:Number = recorder.microphone.activityLevel;
  100. TweenMax.to(soundMeter, .1, {scaleX:al * .01, onUpdate:onActivitylevelUpdate});//, onUpdateParams:[al]})
  101. if (!recording) recording = true;
  102. }
  103. public function onActivitylevelUpdate(al)
  104. {
  105. //statustxt.text = _activityLevel
  106. // draw a cool sine wave!
  107. xpos += speedX;
  108. ypos = centerY + Math.sin(angle) * amplitude * ((al > 20)? al / 100 : 1)
  109. angle += speedAngle;
  110. graphics.lineTo(xpos,ypos)
  111. }
  112. private function onRecordComplete(e:Event):void
  113. {
  114. soundMeter.scaleX = 0
  115.  
  116. recording = false;
  117. statustxt.text = "recording complete"
  118.  
  119. var src = WaveFile.createSample(recorder.output) // this is fine
  120.  
  121. // I think im not clearing out the old audio properly here somehow...
  122. var sequence = new ListPerformance()
  123. sequence.addSourceAt(0, src)
  124. var ap = new AudioPerformer(sequence, new AudioDescriptor())
  125. //player.play(ap)
  126.  
  127. renderWav(ap, true)
  128.  
  129. // save to wav?
  130. // new FileReference().save (recorder.output, "VOCariousRecording.wav")
  131. }
  132. private function renderWav(src, convertToMp3 = false)
  133. {
  134. var innerTimer = new Timer(10,0)
  135. var framesPerChunk:uint = 8192;
  136.  
  137. innerTimer.addEventListener(TimerEvent.TIMER, handleRenderTimer)
  138. innerTimer.start()
  139.  
  140. function handleRenderTimer(e:TimerEvent)
  141. {
  142. src.getSample(framesPerChunk).writeWavBytes(myWavData)
  143.  
  144. var m = Math.min(src.frameCount, src.position + framesPerChunk)
  145. var n = Math.max(0, m - src.position)
  146.  
  147. if (n == 0)
  148. {
  149. if (src.position > 0) finishRender() else trace("cancel rendering")
  150.  
  151. } else {
  152. statustxt.text = "rendering audio: "+ Math.floor(src.position * 100 / src.frameCount) + "%";
  153. }
  154. }
  155. function finishRender()
  156. {
  157. innerTimer.stop()
  158. statustxt.text = "finishing audio render"
  159. WaveFile.writeBytesToWavFile(myWavFile, myWavData, 44100, 2, 16)
  160.  
  161. if (!convertToMp3)
  162. {
  163. wavbtn.enabled = true;
  164. } else {
  165. makeIntoMp3(myWavFile)
  166. }
  167. }
  168. }
  169. private function makeIntoMp3(wav)
  170. {
  171. wav.position = 0
  172. mp3Encoder = new ShineMP3Encoder(wav);
  173. mp3Encoder.addEventListener(Event.COMPLETE, mp3EncodeComplete);
  174. mp3Encoder.addEventListener(ProgressEvent.PROGRESS, mp3EncodeProgress);
  175. //mp3Encoder.addEventListener(ErrorEvent.ERROR, mp3EncodeError);
  176. mp3Encoder.start();
  177.  
  178. function mp3EncodeProgress(e:ProgressEvent) : void
  179. {
  180. statustxt.text = "encoding mp3: " + e.bytesLoaded + "%"
  181. }
  182.  
  183. function mp3EncodeComplete(e: Event) : void
  184. {
  185. statustxt.text = "mp3 encoding completen"
  186. statustxt.appendText("you can now save the mp3 to your desktop")
  187. wavbtn.enabled = true;
  188. }
  189. }
  190. private function onWavClick(e:MouseEvent)
  191. {
  192. // WRITE ID3 TAGS
  193. var sba:ByteArray = mp3Encoder.mp3Data;
  194. sba.position = sba.length - 128
  195. sba.writeMultiByte("TAG", "iso-8859-1");
  196. sba.writeMultiByte("Microphone Test 1-2, 1-2 "+String.fromCharCode(0), "iso-8859-1"); // Title
  197. sba.writeMultiByte("jordansthings "+String.fromCharCode(0), "iso-8859-1"); // Artist
  198. sba.writeMultiByte("Jordan's Thingz Bop Volume 1 "+String.fromCharCode(0), "iso-8859-1"); // Album
  199. sba.writeMultiByte("2010" + String.fromCharCode(0), "iso-8859-1"); // Year
  200. sba.writeMultiByte("www.jordansthings.com " + String.fromCharCode(0), "iso-8859-1");// comments
  201. sba.writeByte(57);
  202.  
  203. new FileReference().save(sba, "FlashMicrophoneTest.mp3")
  204. }
  205. private function onPlayComplete(e:Event)
  206. {
  207. statustxt.text = "playing complete";
  208. }
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement