【问题标题】:Music Visualizer using Processing使用处理的音乐可视化器
【发布时间】:2021-12-29 12:33:59
【问题描述】:

我正在尝试使用 Processing 创建一个音乐可视化工具。我的想法是在整个屏幕上画一条线,随着歌曲的变化而移动。

这就是我现在拥有的

import ddf.minim.analysis.*;
import ddf.minim.*;

Minim minim;
AudioPlayer track;
AudioInput input;
FFT fft;
//AudioIn in;
String audioFileName = "dune.mp3";

void setup()
{
  //size(480, 320);
  fullScreen();
  noCursor();
  minim = new Minim(this);
  track = minim.loadFile(audioFileName, 2048);
  input = minim.getLineIn();

  fft = new FFT(input.bufferSize(), input.sampleRate());
}

void draw()
{
  background(0);
  stroke(255);

  fft.forward(input.mix);
 
  for (int i = 0; i < fft.specSize(); i++)
  {

    ellipse(i, 200, 7, fft.getBand(i)*10);
    //line(0, 200, 200, fft.getBand(i));

   
  }
}

所以,我的问题是:

  1. 我正在加载的音乐无法正常工作。 (字符串音频文件名)。只有输入,所以当我对着麦克风说话时,它可以工作
  2. 我无法得到一条线,只有像代码这样的椭圆

有谁知道如何解决这个问题?或者有我可以学习的好教程?

谢谢

【问题讨论】:

    标签: processing audio-player visualizer


    【解决方案1】:

    您的演示在我的 Mac 上崩溃了,需要我一段时间来调试。也许我从网上复制的以下演示可以帮助您入门(请参阅文件头):http://code.compartmental.net/minim/minim_class_minim.html。它有几个'println'调用,希望能给你一些关于它是如何工作的想法。您需要在处理草图文件夹中创建一个“数据”文件夹,并将一个名为“groove.mp3”的文件放入数据文件夹中以运行它。我已经在我的系统上对其进行了测试,它似乎工作正常。不使用 FFT。

    /*
     This sketch demonstrates how to play a file with Minim using an AudioPlayer.
     It's also a good example of how to draw the waveform of the audio. Full documentation 
     for AudioPlayer can be found at http://code.compartmental.net/minim/audioplayer_class_audioplayer.html
     For more information about Minim and additional features, visit http://code.compartmental.net/minim/
     */
    
    import ddf.minim.*;
    
    Minim minim;
    AudioPlayer player;
    
    void setup() {
      size(512, 200);
      // we pass this to Minim so that it can load files from the data directory
      minim = new Minim(this);
      println("minim = ", minim);
      // loadFile will look in all the same places as loadImage does.
      // this means you can find files that are in the data folder and the 
      // sketch folder. you can also pass an absolute path, or a URL.
      player = minim.loadFile("groove.mp3");
      println("player = ", player);
    }
    
    void draw() {
      background(0);
      stroke(255);
      // draw the waveforms
      // the values returned by left.get() and right.get() will be between -1 and 1,
      // so we need to scale them up to see the waveform
      // note that if the file is MONO, left.get() and right.get() will return the same value
      for (int i = 0; i < player.bufferSize() - 1; i++) {
        float x1 = map( i, 0, player.bufferSize(), 0, width );
        float x2 = map( i+1, 0, player.bufferSize(), 0, width );
        line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
        line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
      }
      // draw a line to show where in the song playback is currently located
      float posx = map(player.position(), 0, player.length(), 0, width);
      stroke(0, 200, 0);
      line(posx, 0, posx, height);
    
      if ( player.isPlaying() ) {
        text("Press any key to pause playback.", 10, 20 );
      } else {
        text("Press any key to start playback.", 10, 20 );
      }
    }
    
    void keyPressed() {
      if ( player.isPlaying() ) {
        player.pause();
      }
      // if the player is at the end of the file,
      // we have to rewind it before telling it to play again
      else if ( player.position() == player.length() ) {
        player.rewind();
        player.play();
      } else {
        player.play();
      }
    }
    
    

    以下是对您最初帖子的修订以及我用来调试它的参考。 “输入”是麦克风,“轨道”是文件;您已将其设置为使用麦克风,然后我将其切换到文件。就像上面的示例一样,您需要在 Processing 草图文件夹中创建一个数据文件夹并插入一个名为“groove.mp3”的文件。

    /*
      Reference: https://github.com/ddf/Minim/blob/v2.2.2/examples/Analysis/SoundSpectrum/SoundSpectrum.pde
    */
    
    import ddf.minim.analysis.*;
    import ddf.minim.*;
    
    Minim minim;
    AudioPlayer track;
    AudioInput input;
    FFT fft;
    
    String audioFileName = "groove.mp3";
    
    void setup() {
      size(480, 320);
      noCursor();
      minim = new Minim(this);
      track = minim.loadFile(audioFileName, 1024);
      input = minim.getLineIn();
      fft = new FFT(track.bufferSize(), track.sampleRate());
      println(fft);
      track.loop();
    }
    
    void draw() {
      background(0);
      stroke(255);
      fft.forward(track.mix);
      for (int i = 0; i < fft.specSize(); i++) {
        ellipse(i, 200, 7, fft.getBand(i)*10);
        // line(0, 200, 200, fft.getBand(i));  //Unable to get this to work
      }
    }
    
    

    【讨论】:

    • 你的演示效果很好,我得到了我想要的外观!谢谢你:)
    • 如果您觉得它有帮助并回答了您的问题,请检查它是否是解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2010-09-14
    • 1970-01-01
    相关资源
    最近更新 更多