Exemple #1
0
  /**
   * Loads the requested file into a MultiChannelBuffer. The buffer's channel count and buffer size
   * will be adjusted to match the file.
   *
   * @shortdesc Loads the requested file into a MultiChannelBuffer.
   * @example Advanced/loadFileIntoBuffer
   * @param filename the file to load
   * @param outBuffer the MultiChannelBuffer to fill with the file's audio samples
   * @return the sample rate of audio samples in outBuffer, or 0 if the load failed.
   * @related MultiChannelBuffer
   */
  public float loadFileIntoBuffer(String filename, MultiChannelBuffer outBuffer) {
    final int readBufferSize = 4096;
    float sampleRate = 0;
    AudioRecordingStream stream = mimp.getAudioRecordingStream(filename, readBufferSize, false);
    if (stream != null) {
      stream.open();
      stream.play();

      sampleRate = stream.getFormat().getSampleRate();
      final int channelCount = stream.getFormat().getChannels();
      // for reading the file in, in chunks.
      MultiChannelBuffer readBuffer = new MultiChannelBuffer(channelCount, readBufferSize);
      // make sure the out buffer is the correct size and type.
      outBuffer.setChannelCount(channelCount);
      // how many samples to read total

      final long totalSampleCount = stream.getSampleFrameLength();
      outBuffer.setBufferSize((int) totalSampleCount);

      // now read in chunks.
      long totalSamplesRead = 0;
      while (totalSamplesRead < totalSampleCount) {
        // is the remainder smaller than our buffer?
        if (totalSampleCount - totalSamplesRead < readBufferSize) {
          readBuffer.setBufferSize((int) (totalSampleCount - totalSamplesRead));
        }

        stream.read(readBuffer);

        // copy data from one buffer to the other.
        for (int i = 0; i < channelCount; ++i) {
          // a faster way to do this would be nice.
          for (int s = 0; s < readBuffer.getBufferSize(); ++s) {
            outBuffer.setSample(i, (int) totalSamplesRead + s, readBuffer.getSample(i, s));
          }
        }

        totalSamplesRead += readBuffer.getBufferSize();
      }

      stream.close();
    } else {
      debug("Unable to load an AudioRecordingStream for " + filename);
    }

    return sampleRate;
  }
Exemple #2
0
  public void setup() {
    size(1200, 200, JAVA2D);
    preview = createGraphics(width, 100);
    minim = new Minim(this);
    mimp = new JSMinim(this);
    mimp.debugOn();
    // specify that we want the audio buffers of the AudioPlayer
    // to be 1024 samples long because our FFT needs to have
    // a power-of-two buffer size and this is a good size.
    jingle = minim.loadFile(filename, 1024);
    // loop the file indefinitely
    jingle.loop();

    // create an FFT object that has a time-domain buffer
    // the same size as jingle's sample buffer
    // note that this needs to be a power of two
    // and that it means the size of the spectrum will be half as large.
    fft = new FFT(jingle.bufferSize(), jingle.sampleRate());
    MultiChannelBuffer sampleBuffer = new MultiChannelBuffer(0, 0); // params don't matter!
    float jingleBuffer = loadFileIntoBuffer(filename, sampleBuffer);
    totalSamples = sampleBuffer.getBufferSize();
    beat = new BeatDetect(4096 * 4, jingle.sampleRate());
    beat.setSensitivity(1);
    beat.detectMode(BeatDetect.FREQ_ENERGY);
    // find ALL beats
    float data[] = new float[2048];
    samples = sampleBuffer.getChannel(1);

    //		for( int i = 0; i < samples.length; i+= data.length ){
    //			int j = Math.min( samples.length-1, i + data.length - 1 );
    ////			System.out.println( "copy up to including " + ( j ) + " / " + samples.length);
    //			System.arraycopy( samples, i, data, 0, 1 + j - i );
    //			beat.detect( data );
    //			if( beat.isOnset() ){
    //				beats.add( i );
    //				System.out.println( i + ": " + beat.isKick() );
    //			}
    //		}

    // pick some starting point ...
    loopEnd = samples.length;
    int start = (int) random(3 * samples.length / 5);
    analyze(start);
    new Thread() {
      public void run() {
        try {
          while (true) {
            Thread.sleep(1);
            // figure it out
            int s = (int) (loopStart * 1000l / jingle.getFormat().getSampleRate());
            int e = (int) (loopEnd * 1000l / jingle.getFormat().getSampleRate());
            if (jingle.position() < s) jingle.cue(s + 10);
            else if (jingle.position() > e) jingle.cue(s);
            else continue;
            Thread.sleep(500);
          }
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }.start();

    //		System.exit(0);
  }