Flash music spectrum Production

Source: Internet
Author: User
Tags addchild

The effect of the three-day music spectrum:

Includes five classes:

MusicControl class, which is used to control music, including the control of music loading and playback.

ID3Control class, used to extract information related to music files, display various information cyclically, and display garbled characters in extracted Chinese. Here, you can use a function to correct the information:

  1. Function EncodeUtf8 (str: String): String {
  2. Var oriByteArr: ByteArray = new ByteArray ();
  3. OriByteArr. writeUTFBytes (str );
  4. Var tempByteArr: ByteArray = new ByteArray ();
  5. For (var I = 0; I <oriByteArr. length; I ++ ){
  6. If (oriByteArr = 194 ){
  7. TempByteArr. writeByte (oriByteArr [I + 1]);
  8. I ++;
  9. } Else if (oriByteArr = 195 ){
  10. TempByteArr. writeByte (oriByteArr [I + 1] + 64 );
  11. I ++;
  12. } Else {
  13. TempByteArr. writeByte (oriByteArr );
  14. }
  15. }
  16. TempByteArr. position = 0;
  17. Return tempByteArr. readMultiByte (tempByteArr. bytesAvailable, "chinese ");
  18. }

Spectrum class, which manages and displays the Spectrum. The Spectrum information is retrieved from the MusicControl class.

  1. SoundMixer. computeSpectrum (_ spectrumArray, true );

The second parameter "true" indicates that the spectrum is extracted, and "false" indicates that the waveform is obtained.


  1. /*
  2. Class function: displays the spectrum.
  3. */
  4. Package net. cdipan. spectrum {
  5. Import flash. display. Sprite;
  6. Import flash. utils. ByteArray;
  7. Import flash. text. TextField;
  8. Import flash. events. MouseEvent;
  9. Import flash.net. navigateToURL;
  10. Import flash.net. URLRequest;
  11. Public class spectrum extends sprite {
  12. // Records the array of Spectrum
  13. Private VaR _ spectrum: array;
  14. // Record display type
  15. Private VaR _ type: int;
  16. // The sprite object used to display the spectrum
  17. Private VaR _ showspectrum: SPRITE;
  18. // The sprite object used to display the columnar Spectrum
  19. Private VaR _ show1: SPRITE;
  20. // The sprite object used to display the wave spectrum
  21. Private VaR _ show2: SPRITE;
  22. // The sprite object used to display the waveform Spectrum
  23. Private VaR _ show3: SPRITE;
  24. // Column object
  25. Private VaR _ column: column;
  26. // Waveform object
  27. Private VaR _ waveform: waveform;
  28. Public Function spectrum (): void {
  29. _ Spectrum = new array (512 );
  30. _ ShowSpectrum = new Sprite ();
  31. _ ShowSpectrum. x = 22;
  32. _ ShowSpectrum. y = 18;
  33. _ Show1 = new Sprite ();
  34. _ Show2 = new Sprite ();
  35. _ Show3 = new Sprite ();
  36. This. addChild (_ showSpectrum );
  37. // Create a columnar Spectrum
  38. CreateColumn (64,3, 1, _ show1 );
  39. CreateColumn (256, 1, 0, _ show2 );
  40. // Create a waveform Spectrum
  41. _ Waveform = new Waveform ();
  42. _ Show3.addChild (_ waveform );
  43. _ ShowSpectrum. addChild (_ show1 );
  44. }
  45. // Obtain the Spectrum Array
  46. Public function getSpectrum (byteArray: ByteArray): void {
  47. For (var I: int = 0; I <512; I ++ ){
  48. _ Spectrum = byteArray. readFloat ();
  49. }
  50. // Send the spectrum data to the column and wave sprite instances
  51. For (var j: int = 0; j <64; j ++ ){
  52. Var temp1: Column = _ show1.getChildByName ("column _" + j. toString () as Column;
  53. Temp1.getSpectrum (_ spectrum [j * 4] + _ spectrum [j * 4 + 1] + _ spectrum [j * 4 + 2] + _ spectrum [j * 4 + 3] + _ spectrum [j * 4 + 256] + _ spectrum [j * 4 + 257] + _ spectrum [j * 4 + 258] + _ spectrum [j * 4 + 259]) * 12.5 );
  54. }
  55. For (var m: int = 0; m <256; m ++ ){
  56. Var temp2: Column = _ show2.getChildByName ("column _" + m. toString () as Column;
  57. Temp2.getSpectrum (_ spectrum [m] + _ spectrum [m + 256]) * 50 );
  58. }
  59. // Send the spectrum data to the waveform's genie instance
  60. _ Waveform. getSpectrum (_ spectrum );
  61. }
  62. // Create a column sprite object
  63. Private function createColumn (f_num: int, f_width: int, f_space: int, target: Sprite): void {
  64. For (var I: int = 0; I <f_num; I ++ ){
  65. _ Column = new Column ();
  66. _ Column. name = "column _" + I. toString ();
  67. _ Column. width = f_width;
  68. _ Column. x = I * (f_width + f_space );
  69. Target. addChild (_ column );
  70. }
  71. }
  72. // Change the style (when the type is 0, the bar is displayed. When the value is 1, the wave is displayed. When the value is 2, the waveform is displayed)
  73. Public function changeType (type: int): void {
  74. Switch (type ){
  75. Case 0:
  76. If (_ showSpectrum. contains (_ show1 )){
  77. _ ShowSpectrum. removeChild (_ show1 );
  78. }
  79. If (_ showSpectrum. contains (_ show2 )){
  80. _ ShowSpectrum. removeChild (_ show2 );
  81. }
  82. If (_ showSpectrum. contains (_ show3 )){
  83. _ ShowSpectrum. removeChild (_ show3 );
  84. }
  85. _ ShowSpectrum. addChild (_ show1 );
  86. Break;
  87. Case 1:
  88. If (_ showSpectrum. contains (_ show1 )){
  89. _ ShowSpectrum. removeChild (_ show1 );
  90. }
  91. If (_ showSpectrum. contains (_ show2 )){
  92. _ ShowSpectrum. removeChild (_ show2 );
  93. }
  94. If (_ showSpectrum. contains (_ show3 )){
  95. _ ShowSpectrum. removeChild (_ show3 );
  96. }
  97. _ ShowSpectrum. addChild (_ show2 );
  98. Break;
  99. Case 2:
  100. If (_ showSpectrum. contains (_ show1 )){
  101. _ ShowSpectrum. removeChild (_ show1 );
  102. }
  103. If (_ showSpectrum. contains (_ show2 )){
  104. _ ShowSpectrum. removeChild (_ show2 );
  105. }
  106. If (_ showSpectrum. contains (_ show3 )){
  107. _ ShowSpectrum. removeChild (_ show3 );
  108. }
  109. _ ShowSpectrum. addChild (_ show3 );
  110. Break;
  111. }
  112. }
  113. }
  114. }

Column class, a stripe Spectrum class. In the Spectrum class, 64 columnar styles are created, and 256 are created in the wave style.


  1. /*
  2. Class function: columnar spectrum.
  3. */
  4. Package net. cdipan. spectrum {
  5. Import flash. display .*;
  6. Import flash. geom .*;
  7. Import flash. events. Event;
  8. Import flash. utils. Timer;
  9. Import flash. events. TimerEvent;
  10. Public class Column extends Sprite {
  11. // Color of the small square on the top
  12. Private const square_color: uint = 0 xffffff;
  13. // Top color
  14. Private const top_color: uint = 0 xffffff;
  15. // Intermediate color
  16. Private const middle_color: uint = 0x8cdcff;
  17. // Bottom color
  18. Private const bottom_color: uint = 0x07f7ff;
  19. // Background genie object
  20. Private var BG_Sp: Sprite;
  21. // Mask genie object
  22. Private var mask_Sp: Sprite;
  23. // Small box genie object
  24. Private var square_Sp: Sprite;
  25. // Record the value of the last spectrum. If it is higher than this time, it will be reduced by one. Otherwise, this value will be set to this value.
  26. Private var oldNum: Number;
  27. // Timer object
  28. Private var myTimer: Timer;
  29. // Record whether small blocks can fall
  30. Private var canDrop: Boolean;
  31. Public function Column (): void {
  32. BG_Sp = new Sprite ();
  33. DrawGradualRect ();
  34. AddChild (BG_Sp );
  35. Mask_Sp = new Sprite ();
  36. Drawmaskrect ();
  37. Addchild (mask_sp );
  38. Square_sp = new sprite ();
  39. Drawsquarerect ();
  40. Square_sp.y = 99;
  41. Square_sp.addeventlistener (event. enter_frame, _ enterframe );
  42. Addchild (square_sp );
  43. // Sets the mask.
  44. Bg_sp.mask = mask_sp;
  45. Oldnum = 0;
  46. Mytimer = new timer (200, 1 );
  47. Mytimer. addeventlistener ("timer", ontimer );
  48. Candrop = false;
  49. }
  50. // Drop the small square
  51. Private function _ enterframe (E: Event): void {
  52. If (candrop ){
  53. Square_sp.y + = 4;
  54. }
  55. If (square_Sp.y> 99 ){
  56. Square_Sp.y = 99;
  57. }
  58. If (square_Sp.y <-1 ){
  59. Square_Sp.y =-1;
  60. }
  61. }
  62. // Draw a gradient chart
  63. Private function drawGradualRect (): void {
  64. Var fillType: String = GradientType. RADIAL;
  65. Var colors: Array = [top_color, middle_color, bottom_color];
  66. Var alphas: Array = [1, 1, 1];
  67. Var ratios: Array = [0x00, 0x7f, 0xff];
  68. BG_Sp.graphics.beginGradientFill (fillType, colors, alphas, ratios );
  69. BG_Sp.graphics.drawRect (0,-1,100, 1 );
  70. BG_Sp.rotation = 90;
  71. }
  72. // Draw a mask image
  73. Private function drawmaskrect (): void {
  74. Mask_sp.graphics.linestyle ();
  75. Mask_sp.graphics.beginfill (0 );
  76. Mask_sp.graphics.drawrect (0,-100, 1 );
  77. Mask_sp.graphics.endfill ();
  78. Mask_sp.y = 100;
  79. }
  80. // Draw small blocks
  81. Private function drawsquarerect (): void {
  82. Square_sp.graphics.linestyle ();
  83. Square_sp.graphics.beginfill (square_color );
  84. Square_sp.graphics.drawrect (0, 0, 1, 1 );
  85. Square_sp.graphics.endfill ();
  86. }
  87. // Receives spectrum data
  88. Public Function getspectrum (Num: Number): void {
  89. If (oldnum> num ){
  90. Oldnum-= 7;
  91. } Else {
  92. Oldnum = num;
  93. If (oldnum! = 0 ){
  94. // Call the small block motion function
  95. Squaremove (oldnum );
  96. }
  97. }
  98. If (oldnum <0 ){
  99. Oldnum = 0;
  100. }
  101. Mask_sp.height = oldnum;
  102. }
  103. // Function for controlling the movement of small blocks
  104. Private function squaremove (Num: Number): void {
  105. If (square_sp.y> 99-oldnum ){
  106. Square_sp.y = 99-num;
  107. Candrop = false;
  108. Mytimer. Reset ();
  109. Mytimer. Start ();
  110. }
  111. }
  112. Public function onTimer (e: TimerEvent): void {
  113. CanDrop = true;
  114. }
  115. }
  116. }

Waveform class, Waveform class, drawn using a line, adding the effect of gradually disappearing tracks.


  1. /*
  2. Class function: waveform.
  3. */
  4. Package net. cdipan. spectrum {
  5. Import flash. display .*;
  6. Import flash. filters .*;
  7. Import flash. geom .*;
  8. Public class Waveform extends Sprite {
  9. // Line color
  10. Private const line_color: uint = 0x07f7ff;
  11. // Create a line sprite object
  12. Private var Line: Sprite;
  13. // Bitmap object used for gradually disappearing tracks
  14. Private var BMP data: BitmapData;
  15. Private var bmp: Bitmap;
  16. // Filter parameters
  17. Private var colorM: ColorMatrixFilter;
  18. Private var blur: BlurFilter;
  19. Private var r: Rectangle;
  20. Private var point;
  21. // Use a number to make filter processing slow
  22. Private var num: int;
  23. Public function Waveform (): void {
  24. Line = new Sprite ();
  25. Bmp data = new BitmapData (256,100, true, 0 );
  26. Bmp = new Bitmap (bmp data );
  27. ColorM = new ColorMatrixFilter ([. 98,0,. 98,0,., 0,]);
  28. Blur = new BlurFilter (7,7, BitmapFilterQuality. LOW );
  29. R = new Rectangle (0, 0, 256,100 );
  30. P = new Point (0, 0 );
  31. // Add a display object
  32. This. blendMode = BlendMode. ADD;
  33. This. addChild (bmp );
  34. This. addchild (line );
  35. }
  36. // Receives spectrum data
  37. Public Function getspectrum (array: array): void {
  38. If (Num % 2 = 0 ){
  39. VaR M: Number = 0;
  40. For (var j = 0; j <256; j + = 2 ){
  41. M + = array [J];
  42. }
  43. If (M! = 0 ){
  44. // When the playback is stopped, no image is drawn, and only a straight line is displayed.
  45. BMP data. Draw (this );
  46. }
  47. BMP data. applyfilter (BMP data, R, P, colorm );
  48. BMP data. applyfilter (BMP data, R, P, blur );
  49. }
  50. Num ++;
  51. Line. Graphics. Clear ();
  52. Line. Graphics. linestyle (1, line_color, 100 );
  53. For (VAR I = 0; I <256; I + = 2 ){
  54. VaR N: Number = array * 50;
  55. If (I! = 0 ){
  56. Line. Graphics. lineto (I, 50-n );
  57. } Else {
  58. Line. Graphics. moveTo (0, 50, N );
  59. }
  60. }
  61. }
  62. }
  63. }

Source File Download:

Flash music spectrum production .rar

 

From: http://www.68design.net/Web-Guide/Flash/45403-1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.