flash on 2009-8-19 hacker_9cxyb.. forked:0favorite:0lines:258license : MIT License modified : 2009-08-19 02:31:54 Embed Tweet 1.// forked from shinseki09's forked from: NoiseMaker 2.// forked from kappaLab's NoiseMaker 3.package 4.{ 5. 6. import flash.display.Sprite 7. import flash.display.Shape 8. import flash.events.Event; 9. import flash.filters.BlurFilter; 10. import flash.filters.GlowFilter; 11. import flash.media.SoundMixer; 12. import flash.utils.ByteArray; 13. 14. import flash.events.SampleDataEvent; 15. import flash.events.TimerEvent; 16. import flash.media.Sound; 17. import flash.utils.Timer; 18. 19. [SWF(backgroundColor=0x000000, frameRate=60)] 20. 21. public class NoiseMaker extends Sprite 22. { 23. 24. private var monitor:Vector.<Shape> = new Vector.<Shape>() 25. 26. public function NoiseMaker() 27. { 28. for (var i:int = 0; i < 256; i++) 29. { 30. var s:Shape = new Shape() 31. s.graphics.beginFill(0xFFFFFF * Math.random()); 32. s.graphics.drawCircle(0, 0, 20) 33. //s.x = Math.sin(i) * (stage.stageWidth/2) + stage.stageWidth/2; 34. //s.y = Math.cos(i) * stage.stageHeight/2 + stage.stageHeight/2; 35. s.x = stage.stageWidth * i / 256 36. s.y = stage.stageHeight/2 37. monitor.push(s) 38. addChild(s) 39. } 40. addEventListener(Event.ENTER_FRAME, onEnterFrame); 41. filters = [new BlurFilter(5, 5)] 42. 43. var count:int = 0 44. function setVol():void 45. { 46. count++ 47. 48. if (count > 240) { 49. count = 1 50. } 51. 52. 53. 54. if (count < 20) return; 55. if (count % 20 == 0) { 56. volB = 2 57. } 58. if (count % 20 == 1 59. || count % 20 == 2 60. || count % 20 == 3 61. || count % 20 == 4 62. || count % 20 == 5 63. || count % 20 == 6) { 64. if(Math.random() > .5) 65. volB2 = 2 66. } 67. if (count < 40) return; 68. 69. if (count % 40 == 0) { 70. volP = 1 71. } 72. if (count % 40 == 5) { 73. volP2 = 1 74. volN = 1 75. } 76. 77. if (count % 40 == 5 78. || count % 40 == 6 79. || count % 40 == 7 80. || count % 40 == 8 81. || count % 40 == 9 82. || count % 40 == 10) { 83. if(Math.random() > .5) 84. volN = 2 85. 86. } 87. 88. if (count < 120) return; 89. 90. if (count % 120 == 0) { 91. volK2 = 1 92. } 93. if (count % 120 == 11 94. || count % 120 == 12 95. || count % 120 == 13 96. || count % 120 == 14 97. || count % 120 == 15 98. || count % 120 == 16) { 99. if(Math.random() > .3) 100. volK = 1 101. } 102. if (count % 120 == 11 103. || count % 120 == 12 104. || count % 120 == 13 105. || count % 120 == 14 106. || count % 120 == 15 107. || count % 120 == 16) { 108. if(Math.random() > .1) 109. volK3 = 1 110. } 111. if (count % 120 == 14 112. || count % 120 == 15 113. || count % 120 == 16 114. || count % 120 == 17 115. || count % 120 == 18 116. || count % 120 == 19) { 117. if(Math.random() > .1) 118. volK2 = 1 119. volP = 1 120. } 121. 122. } 123. 124. var volB:Number = 0 125. var volB2:Number = 0 126. var volK:Number = 0 127. var volR:Number = 0 128. var volP:Number = 0 129. var volP2:Number = 0 130. var volN:Number = 0 131. var volN2:Number = 0 132. var volK2:Number = 0 133. var volK3:Number = 0 134. 135. var mySound:Sound = new Sound(); 136. function gen(e:SampleDataEvent):void { 137. 138. setVol() 139. 140. for ( var c:int = 0; c < 2048; c++ ) { 141. 142. var i:Number = Number(c + e.position) 143. var arr:Vector.<Number> = new Vector.<Number>() 144. 145. var bass:Number = 0; 146. volB += (0 - volB) * .0001; 147. bass = sine(i,60); 148. bass *= volB 149. arr.push(bass) 150. 151. var bass2:Number = 0; 152. volB2 += (0 - volB2) * .0008; 153. bass2 = sine(i,20) ; 154. bass2 *= volB2; 155. arr.push(bass2) 156. 157. var kick:Number = 0; 158. volK += (0 - volK) * .0001; 159. kick = triangle(i, 60); 160. kick = (kick < .1)?0:kick; 161. kick *= (volK > .5)?1:volK; 162. arr.push(kick) 163. 164. var kick2:Number = 0; 165. volK2 += (0 - volK2) * .0008; 166. kick2 = square(i, .5) + square(i-1, 5) + noise(); 167. kick2 *= volK2 168. arr.push(kick2) 169. 170. var kick3:Number = 0; 171. volK3 += (0 - volK3) * .0006; 172. kick3 = triangle(i, 2) + square(i,1) 173. kick3 *= volK3 174. arr.push(kick3) 175. 176. var piano:Number = 0; 177. volP += (0 - volP) * .0001; 178. piano = sine(i,2.5) 179. piano *= volP 180. arr.push(piano) 181. 182. var piano2:Number = 0; 183. volP2 += (0 - volP2) * .0001; 184. piano2 = sine(i,2.75) 185. piano2 *= volP2 186. arr.push(piano2) 187. 188. var nis:Number = 0 189. volN += (0 - volN) * .0005; 190. nis = noise() 191. nis *= volN; 192. arr.push(nis) 193. 194. var nis2:Number = 0 195. volN2 += (0 - volN2) * .001 ; 196. nis2 = noise() + square(i,.5) 197. nis2 *= volN2; 198. arr.push(nis2) 199. 200. var smpl:Number = 0 201. for each (var p:Number in arr) { 202. smpl += p 203. } 204. e.data.writeFloat(smpl); 205. e.data.writeFloat(smpl); 206. } 207. } 208. 209. mySound.addEventListener(SampleDataEvent.SAMPLE_DATA,gen); 210. mySound.play(); 211. 212. } 213. 214. private function onEnterFrame(e:Event):void 215. { 216. var bytes:ByteArray = new ByteArray(); 217. SoundMixer.computeSpectrum(bytes, false, 0); 218. 219. if (bytes.length == 1) return 220. //trace(bytes.readFloat()) 221. bytes.position = 0 222. 223. for (var i:int = 0; i < 256 ; i++ ){ 224. var sample:Number = bytes.readFloat() 225. monitor[i].scaleX = sample * 1 226. monitor[i].scaleY = sample * 1 227. } 228. } 229. private function sine(i:Number,f:Number):Number 230. { 231. var smpl:Number = Math.sin(i / Math.PI / f); 232. return smpl 233. } 234. private function square(i:Number, f:Number):Number 235. { 236. var smpl:Number = Math.sin(i/Math.PI/f); 237. smpl = (smpl > 0)?1: -1; 238. return smpl 239. } 240. private function saw(i:Number,f:Number):Number 241. { 242. f *= 25 243. var smpl:Number = 1 - (i % f) / f * 2; 244. return smpl 245. } 246. private function triangle(i:Number,f:Number):Number 247. { 248. f *= 25 249. var smpl:Number =Math.abs(((i) % f) / f - .5) * 4 - 1; 250. return smpl 251. } 252. private function noise():Number 253. { 254. return Math.random() * 2 - 1; 255. } 256. } 257. 258.} Code Fullscreen Preview Fullscreen push Shape Sound filters scaleY scaleX play addEventListener TimerEvent.ENTER_FRAME Math.abs Math.PI Math.sin Vector Math.random length int Number