极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5988|回复: 1

Processing之旅-----【给鱼插上翅膀,Processing基础扩展库2】

[复制链接]
发表于 2013-7-8 17:37:13 | 显示全部楼层 |阅读模式
下面我们继续学习Processing自带的库---minim ,这是一个音频处理库,怎么说功能十分强大,官网是http://code.compartmental.net/tools/minim/

  1. /**
  2.   * This is a relatively simple file player that lets you scrub forward and backward in an audio file.<br />
  3.   * It should be noted that it's not *exactly* scrubbing because the playback speed is not changed,
  4.   * it's simply that the position in the song is changed by very small increments when fast-forwarding or rewinding.
  5.   * But the end result is convincing enough.
  6.   * <p>
  7.   * The positioning code is inside of the Play, Rewind, and Forward classes, which are in button.pde.
  8.   */

  9. import ddf.minim.*;

  10. Minim minim;
  11. AudioPlayer song;
  12. Play play;
  13. Rewind rewind;
  14. Forward ffwd;

  15. void setup()
  16. {
  17.   size(512, 200, P3D);
  18.   minim = new Minim(this);
  19.   // load a file from the data folder, use a sample buffer of 1024 samples
  20.   song = minim.loadFile("fair1939.wav", 512);
  21.   // buttons for control
  22.   play = new Play(width/2 - 50, 130, 20, 10);
  23.   rewind = new Rewind(width/2, 130, 20, 10);
  24.   ffwd = new Forward(width/2 + 50, 130, 20, 10);
  25. }

  26. void draw()
  27. {
  28.   background(0);
  29.   // draw the wave form
  30.   // this wav is MONO, so we only need the left channel,
  31.   // though we could have used the right channel and gotten the same values
  32.   stroke(255);
  33.   for (int i = 0; i < song.bufferSize() - 1;  i++)
  34.   {
  35.     line(i, 50 - song.left.get(i)*50, i+1, 50 - song.left.get(i+1)*10);
  36.   }
  37.   // draw the position in the song
  38.   // the position is in milliseconds,
  39.   // to get a meaningful graphic, we need to map the value to the range [0, width]
  40.   float x = map(song.position(), 0, song.length(), 0, width);
  41.   stroke(255, 0, 0);
  42.   line(x, 50 - 20, x, 50 + 20);
  43.   // do the controls
  44.   play.update();
  45.   play.draw();
  46.   rewind.update();
  47.   rewind.draw();
  48.   ffwd.update();
  49.   ffwd.draw();
  50. }

  51. void mousePressed()
  52. {
  53.   play.mousePressed();
  54.   rewind.mousePressed();
  55.   ffwd.mousePressed();
  56. }

  57. void mouseReleased()
  58. {
  59.   play.mouseReleased();
  60.   rewind.mouseReleased();
  61.   ffwd.mouseReleased();
  62. }
复制代码

  1. abstract class Button
  2. {
  3.   int x, y, hw, hh;
  4.   
  5.   Button(int x, int y, int hw, int hh)
  6.   {
  7.     this.x = x;
  8.     this.y = y;
  9.     this.hw = hw;
  10.     this.hh = hh;
  11.   }
  12.   
  13.   boolean pressed()
  14.   {
  15.     return mouseX > x - hw && mouseX < x + hw && mouseY > y - hh && mouseY < y + hh;
  16.   }
  17.   
  18.   abstract void mousePressed();
  19.   abstract void mouseReleased();
  20.   abstract void update();
  21.   abstract void draw();
  22. }

  23. class Play extends Button
  24. {
  25.   boolean play;
  26.   boolean invert;
  27.   
  28.   Play(int x, int y, int hw, int hh)
  29.   {
  30.     super(x, y, hw, hh);
  31.     play = true;
  32.   }
  33.   
  34.   // code to handle playing and pausing the file
  35.   void mousePressed()
  36.   {
  37.     if ( pressed() )
  38.     {
  39.       invert = true;
  40.       if ( song.isPlaying() )
  41.       {
  42.         song.pause();
  43.         play = true;
  44.       }
  45.       else
  46.       {
  47.         song.loop();
  48.         play = false;
  49.       }
  50.     }
  51.   }
  52.   
  53.   void mouseReleased()
  54.   {
  55.     invert = false;
  56.   }
  57.   
  58.   // play is a boolean value used to determine what to draw on the button
  59.   void update()
  60.   {
  61.     if ( song.isPlaying() ) play = false;
  62.     else play = true;
  63.   }
  64.   
  65.   void draw()
  66.   {
  67.     if ( invert )
  68.     {
  69.       fill(255);
  70.       stroke(0);
  71.     }
  72.     else
  73.     {
  74.       noFill();
  75.       stroke(255);
  76.     }
  77.     rect(x - hw, y - hh, hw*2, hh*2);
  78.     if ( invert )
  79.     {
  80.       fill(0);
  81.       stroke(255);
  82.     }
  83.     else
  84.     {
  85.       fill(255);
  86.       noStroke();
  87.     }
  88.     if ( play )
  89.     {
  90.       triangle(x - hw/3, y - hh/2, x - hw/3, y + hh/2, x + hw/2, y);
  91.     }
  92.     else
  93.     {
  94.       rect(x - hw/3, y - hh/2, hw/4, hh);
  95.       rect(x + hw/8, y - hh/2, hw/4, hh);
  96.     }
  97.   }
  98. }

  99. class Rewind extends Button
  100. {
  101.   boolean invert;
  102.   boolean pressed;
  103.   
  104.   Rewind(int x, int y, int hw, int hh)
  105.   {
  106.     super(x, y, hw, hh);
  107.     invert = false;
  108.   }
  109.   
  110.   // code used to scrub backward in the file
  111.   void update()
  112.   {
  113.     // if the rewind button is currently being pressed
  114.     if (pressed)
  115.     {
  116.       // get the current song position
  117.       int pos = song.position();
  118.       // if it greater than 200 milliseconds
  119.       if ( pos > 200 )
  120.       {
  121.         // rewind the song by 200 milliseconds
  122.         song.skip(-200);
  123.       }
  124.       else
  125.       {
  126.         // if the song hasn't played more than 100 milliseconds
  127.         // just rewind to the beginning
  128.         song.rewind();
  129.       }
  130.     }
  131.   }
  132.   
  133.   void mousePressed()
  134.   {
  135.     pressed = pressed();
  136.     if ( pressed )
  137.     {
  138.       invert = true;
  139.       // if the song isn't currently playing, rewind it to the beginning
  140.       if ( !song.isPlaying() ) song.rewind();      
  141.     }
  142.   }
  143.   
  144.   void mouseReleased()
  145.   {
  146.     pressed = false;
  147.     invert = false;
  148.   }

  149.   void draw()
  150.   {
  151.     if ( invert )
  152.     {
  153.       fill(255);
  154.       stroke(0);
  155.     }
  156.     else
  157.     {
  158.       noFill();
  159.       stroke(255);
  160.     }
  161.     rect(x - hw, y - hh, hw*2, hh*2);
  162.     if ( invert )
  163.     {
  164.       fill(0);
  165.       stroke(255);
  166.     }
  167.     else
  168.     {
  169.       fill(255);
  170.       noStroke();
  171.     }
  172.     triangle(x - hw/2, y, x, y - hh/2, x, y + hh/2);
  173.     triangle(x, y, x + hw/2, y - hh/2, x + hw/2, y + hh/2);   
  174.   }  
  175. }

  176. class Forward extends Button
  177. {
  178.   boolean invert;
  179.   boolean pressed;
  180.   
  181.   Forward(int x, int y, int hw, int hh)
  182.   {
  183.     super(x, y, hw, hh);
  184.     invert = false;
  185.   }
  186.   
  187.   void update()
  188.   {
  189.     // if the forward button is currently being pressed
  190.     if (pressed)
  191.     {
  192.       // get the current position of the song
  193.       int pos = song.position();
  194.       // if the song's position is more than 40 milliseconds from the end of the song
  195.       if ( pos < song.length() - 40 )
  196.       {
  197.         // forward the song by 40 milliseconds
  198.         song.skip(40);
  199.       }
  200.       else
  201.       {
  202.         // otherwise, cue the song at the end of the song
  203.         song.cue( song.length() );
  204.       }
  205.       // start the song playing
  206.       song.play();
  207.     }
  208.   }
  209.   
  210.   void mousePressed()
  211.   {
  212.     pressed = pressed();
  213.     if ( pressed )
  214.     {
  215.       invert = true;      
  216.     }
  217.   }
  218.   
  219.   void mouseReleased()
  220.   {
  221.     pressed = false;
  222.     invert = false;
  223.   }

  224.   void draw()
  225.   {
  226.     if ( invert )
  227.     {
  228.       fill(255);
  229.       stroke(0);
  230.     }
  231.     else
  232.     {
  233.       noFill();
  234.       stroke(255);
  235.     }
  236.     rect(x - hw, y - hh, hw*2, hh*2);
  237.     if ( invert )
  238.     {
  239.       fill(0);
  240.       stroke(255);
  241.     }
  242.     else
  243.     {
  244.       fill(255);
  245.       noStroke();
  246.     }
  247.     triangle(x, y, x - hw/2, y - hh/2, x - hw/2, y + hh/2);
  248.     triangle(x, y - hh/2, x, y + hh/2, x + hw/2, y);   
  249.   }  
  250. }
复制代码

以上是一个官方的例子,很多minim的功能都要参照其官网的文档。

举一个比较使用的例子
如果游戏中我们想处理一个音效,让它只出现一次,song.loop(0);就可以了。不用搞太复杂。
回复

使用道具 举报

发表于 2016-6-22 16:52:30 | 显示全部楼层
谢谢楼主,学习了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2019-8-20 07:32 , Processed in 0.043428 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表