Chris@1: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1:
Chris@1: /*
Chris@1: Vamp feature extraction plugin for the BeatRoot beat tracker.
Chris@1:
Chris@1: Centre for Digital Music, Queen Mary, University of London.
Chris@1: This file copyright 2011 Simon Dixon, Chris Cannam and QMUL.
Chris@1:
Chris@1: This program is free software; you can redistribute it and/or
Chris@1: modify it under the terms of the GNU General Public License as
Chris@1: published by the Free Software Foundation; either version 2 of the
Chris@1: License, or (at your option) any later version. See the file
Chris@1: COPYING included with this distribution for more information.
Chris@1: */
Chris@1:
Chris@1: #ifndef _BEATROOT_PROCESSOR_H_
Chris@1: #define _BEATROOT_PROCESSOR_H_
Chris@1:
Chris@1: class BeatRootProcessor
Chris@1: {
Chris@1: protected:
Chris@1: /** Sample rate of audio */
Chris@1: float sampleRate;
Chris@1:
Chris@1: /** Spacing of audio frames (determines the amount of overlap or
Chris@1: * skip between frames). This value is expressed in
Chris@1: * seconds. (Default = 0.020s) */
Chris@1: double hopTime;
Chris@1:
Chris@1: /** The approximate size of an FFT frame in seconds. (Default =
Chris@1: * 0.04644s). The value is adjusted so that fftSize
Chris@1: * is always power of 2. */
Chris@1: double fftTime;
Chris@1:
Chris@1: /** Spacing of audio frames in samples (see hopTime
) */
Chris@1: int hopSize;
Chris@1:
Chris@1: /** The size of an FFT frame in samples (see fftTime
) */
Chris@1: int fftSize;
Chris@1:
Chris@1: /** The number of overlapping frames of audio data which have been read. */
Chris@1: int frameCount;
Chris@1:
Chris@1: /** RMS amplitude of the current frame. */
Chris@1: double frameRMS;
Chris@1:
Chris@1: /** Long term average frame energy (in frequency domain representation). */
Chris@1: double ltAverage;
Chris@1:
Chris@1: /** Spectral flux onset detection function, indexed by frame. */
Chris@1: vector spectralFlux;
Chris@1:
Chris@1: /** A mapping function for mapping FFT bins to final frequency bins.
Chris@1: * The mapping is linear (1-1) until the resolution reaches 2 points per
Chris@1: * semitone, then logarithmic with a semitone resolution. e.g. for
Chris@1: * 44.1kHz sampling rate and fftSize of 2048 (46ms), bin spacing is
Chris@1: * 21.5Hz, which is mapped linearly for bins 0-34 (0 to 732Hz), and
Chris@1: * logarithmically for the remaining bins (midi notes 79 to 127, bins 35 to
Chris@1: * 83), where all energy above note 127 is mapped into the final bin. */
Chris@1: vector freqMap;
Chris@1:
Chris@1: /** The number of entries in freqMap
. Note that the length of
Chris@1: * the array is greater, because its size is not known at creation time. */
Chris@1: int freqMapSize;
Chris@1:
Chris@1: /** The magnitude spectrum of the most recent frame. Used for
Chris@1: * calculating the spectral flux. */
Chris@1: vector prevFrame;
Chris@1:
Chris@1: /** The magnitude spectrum of the current frame. */
Chris@1: vector newFrame;
Chris@1:
Chris@1: /** The magnitude spectra of all frames, used for plotting the spectrogram. */
Chris@1: vector > frames; //!!! do we need this? much cheaper to lose it if we don't
Chris@1:
Chris@1: /** The RMS energy of all frames. */
Chris@1: vector energy; //!!! unused in beat tracking?
Chris@1:
Chris@1: /** The estimated onset times from peak-picking the onset
Chris@1: * detection function(s). */
Chris@1: vector onsets;
Chris@1:
Chris@1: /** The estimated onset times and their saliences. */
Chris@1: //!!!EventList onsetList;
Chris@1: vector onsetList; //!!! corresponding to keyDown member of events in list
Chris@1:
Chris@1: /** Total number of audio frames if known, or -1 for live or compressed input. */
Chris@1: int totalFrames;
Chris@1:
Chris@1: /** Flag for enabling or disabling debugging output */
Chris@1: static bool debug = false;
Chris@1:
Chris@1: /** Flag for suppressing all standard output messages except results. */
Chris@1: static bool silent = true;
Chris@1:
Chris@1: /** RMS frame energy below this value results in the frame being
Chris@1: * set to zero, so that normalisation does not have undesired
Chris@1: * side-effects. */
Chris@1: static double silenceThreshold = 0.0004; //!!!??? energy of what? should not be static?
Chris@1:
Chris@1: /** For dynamic range compression, this value is added to the log
Chris@1: * magnitude in each frequency bin and any remaining negative
Chris@1: * values are then set to zero.
Chris@1: */
Chris@1: static double rangeThreshold = 10; //!!! sim
Chris@1:
Chris@1: /** Determines method of normalisation. Values can be:
Chris@1: * - 0: no normalisation
Chris@1: * - 1: normalisation by current frame energy
Chris@1: * - 2: normalisation by exponential average of frame energy
Chris@1: *
Chris@1: */
Chris@1: static int normaliseMode = 2;
Chris@1:
Chris@1: /** Ratio between rate of sampling the signal energy (for the
Chris@1: * amplitude envelope) and the hop size */
Chris@1: static int energyOversampleFactor = 2; //!!! not used?
Chris@1:
Chris@1: public:
Chris@1:
Chris@1: /** Constructor: note that streams are not opened until the input
Chris@1: * file is set (see setInputFile()
). */
Chris@1: AudioProcessor() {
Chris@1: cbIndex = 0;
Chris@1: frameRMS = 0;
Chris@1: ltAverage = 0;
Chris@1: frameCount = 0;
Chris@1: hopSize = 0;
Chris@1: fftSize = 0;
Chris@1: hopTime = 0.010; // DEFAULT, overridden with -h
Chris@1: fftTime = 0.04644; // DEFAULT, overridden with -f
Chris@1: progressCallback = null;
Chris@1: stdIn = new BufferedReader(new InputStreamReader(System.in));
Chris@1: if (doOnsetPlot)
Chris@1: plot = new Plot();
Chris@1: } // constructor
Chris@1:
Chris@1: /** For debugging, outputs information about the AudioProcessor to
Chris@1: * standard error.
Chris@1: */
Chris@1: public void print() {
Chris@1: System.err.println(this);
Chris@1: } // print()
Chris@1:
Chris@1: /** For interactive pause - wait for user to hit Enter */
Chris@1: public String readLine() {
Chris@1: try { return stdIn.readLine(); } catch (Exception e) { return null; }
Chris@1: } // readLine()
Chris@1:
Chris@1: /** Gives some basic information about the audio being processed. */
Chris@1: public String toString() {
Chris@1: return "AudioProcessor\n" +
Chris@1: String.format("\tFile: %s (%3.1f kHz, %1d channels)\n",
Chris@1: audioFileName, sampleRate/1000, channels) +
Chris@1: String.format("\tHop / FFT sizes: %5.3f / %5.3f",
Chris@1: hopTime, hopTime * fftSize / hopSize);
Chris@1: } // toString()
Chris@1:
Chris@1: /** Adds a link to the GUI component which shows the progress of matching.
Chris@1: * @param c the AudioProcessor representing the other performance
Chris@1: */
Chris@1: public void setProgressCallback(ProgressIndicator c) {
Chris@1: progressCallback = c;
Chris@1: } // setProgressCallback()
Chris@1:
Chris@1: /** Sets up the streams and buffers for live audio input (CD quality).
Chris@1: * If any Exception is thrown within this method, it is caught, and any
Chris@1: * opened streams are closed, and pcmInputStream
is set to
Chris@1: * null
, indicating that the method did not complete
Chris@1: * successfully.
Chris@1: */
Chris@1: public void setLiveInput() {
Chris@1: try {
Chris@1: channels = 2;
Chris@1: sampleRate = 44100;
Chris@1: AudioFormat desiredFormat = new AudioFormat(
Chris@1: AudioFormat.Encoding.PCM_SIGNED, sampleRate, 16,
Chris@1: channels, channels * 2, sampleRate, false);
Chris@1: TargetDataLine tdl = AudioSystem.getTargetDataLine(desiredFormat);
Chris@1: tdl.open(desiredFormat, liveInputBufferSize);
Chris@1: pcmInputStream = new AudioInputStream(tdl);
Chris@1: audioFormat = pcmInputStream.getFormat();
Chris@1: init();
Chris@1: tdl.start();
Chris@1: } catch (Exception e) {
Chris@1: e.printStackTrace();
Chris@1: closeStreams(); // make sure it exits in a consistent state
Chris@1: }
Chris@1: } // setLiveInput()
Chris@1:
Chris@1: /** Sets up the streams and buffers for audio file input.
Chris@1: * If any Exception is thrown within this method, it is caught, and any
Chris@1: * opened streams are closed, and pcmInputStream
is set to
Chris@1: * null
, indicating that the method did not complete
Chris@1: * successfully.
Chris@1: * @param fileName The path name of the input audio file.
Chris@1: */
Chris@1: public void setInputFile(String fileName) {
Chris@1: closeStreams(); // release previously allocated resources
Chris@1: audioFileName = fileName;
Chris@1: try {
Chris@1: if (audioFileName == null)
Chris@1: throw new Exception("No input file specified");
Chris@1: File audioFile = new File(audioFileName);
Chris@1: if (!audioFile.isFile())
Chris@1: throw new FileNotFoundException(
Chris@1: "Requested file does not exist: " + audioFileName);
Chris@1: rawInputStream = AudioSystem.getAudioInputStream(audioFile);
Chris@1: audioFormat = rawInputStream.getFormat();
Chris@1: channels = audioFormat.getChannels();
Chris@1: sampleRate = audioFormat.getSampleRate();
Chris@1: pcmInputStream = rawInputStream;
Chris@1: if ((audioFormat.getEncoding()!=AudioFormat.Encoding.PCM_SIGNED) ||
Chris@1: (audioFormat.getFrameSize() != channels * 2) ||
Chris@1: audioFormat.isBigEndian()) {
Chris@1: AudioFormat desiredFormat = new AudioFormat(
Chris@1: AudioFormat.Encoding.PCM_SIGNED, sampleRate, 16,
Chris@1: channels, channels * 2, sampleRate, false);
Chris@1: pcmInputStream = AudioSystem.getAudioInputStream(desiredFormat,
Chris@1: rawInputStream);
Chris@1: audioFormat = desiredFormat;
Chris@1: }
Chris@1: init();
Chris@1: } catch (Exception e) {
Chris@1: e.printStackTrace();
Chris@1: closeStreams(); // make sure it exits in a consistent state
Chris@1: }
Chris@1: } // setInputFile()
Chris@1:
Chris@1: /** Allocates memory for arrays, based on parameter settings */
Chris@1: protected void init() {
Chris@1: hopSize = (int) Math.round(sampleRate * hopTime);
Chris@1: fftSize = (int) Math.round(Math.pow(2,
Chris@1: Math.round( Math.log(fftTime * sampleRate) / Math.log(2))));
Chris@1: makeFreqMap(fftSize, sampleRate);
Chris@1: int buffSize = hopSize * channels * 2;
Chris@1: if ((inputBuffer == null) || (inputBuffer.length != buffSize))
Chris@1: inputBuffer = new byte[buffSize];
Chris@1: if ((circBuffer == null) || (circBuffer.length != fftSize)) {
Chris@1: circBuffer = new double[fftSize];
Chris@1: reBuffer = new double[fftSize];
Chris@1: imBuffer = new double[fftSize];
Chris@1: prevPhase = new double[fftSize];
Chris@1: prevPrevPhase = new double[fftSize];
Chris@1: prevFrame = new double[fftSize];
Chris@1: window = FFT.makeWindow(FFT.HAMMING, fftSize, fftSize);
Chris@1: for (int i=0; i < fftSize; i++)
Chris@1: window[i] *= Math.sqrt(fftSize);
Chris@1: }
Chris@1: if (pcmInputStream == rawInputStream)
Chris@1: totalFrames = (int)(pcmInputStream.getFrameLength() / hopSize);
Chris@1: else
Chris@1: totalFrames = (int) (MAX_LENGTH / hopTime);
Chris@1: if ((newFrame == null) || (newFrame.length != freqMapSize)) {
Chris@1: newFrame = new double[freqMapSize];
Chris@1: frames = new double[totalFrames][freqMapSize];
Chris@1: } else if (frames.length != totalFrames)
Chris@1: frames = new double[totalFrames][freqMapSize];
Chris@1: energy = new double[totalFrames*energyOversampleFactor];
Chris@1: phaseDeviation = new double[totalFrames];
Chris@1: spectralFlux = new double[totalFrames];
Chris@1: frameCount = 0;
Chris@1: cbIndex = 0;
Chris@1: frameRMS = 0;
Chris@1: ltAverage = 0;
Chris@1: progressCallback = null;
Chris@1: } // init()
Chris@1:
Chris@1: /** Closes the input stream(s) associated with this object. */
Chris@1: public void closeStreams() {
Chris@1: if (pcmInputStream != null) {
Chris@1: try {
Chris@1: pcmInputStream.close();
Chris@1: if (pcmInputStream != rawInputStream)
Chris@1: rawInputStream.close();
Chris@1: if (audioOut != null) {
Chris@1: audioOut.drain();
Chris@1: audioOut.close();
Chris@1: }
Chris@1: } catch (Exception e) {}
Chris@1: pcmInputStream = null;
Chris@1: audioOut = null;
Chris@1: }
Chris@1: } // closeStreams()
Chris@1:
Chris@1: /** Creates a map of FFT frequency bins to comparison bins.
Chris@1: * Where the spacing of FFT bins is less than 0.5 semitones, the mapping is
Chris@1: * one to one. Where the spacing is greater than 0.5 semitones, the FFT
Chris@1: * energy is mapped into semitone-wide bins. No scaling is performed; that
Chris@1: * is the energy is summed into the comparison bins. See also
Chris@1: * processFrame()
Chris@1: */
Chris@1: protected void makeFreqMap(int fftSize, float sampleRate) {
Chris@1: freqMap = new int[fftSize/2+1];
Chris@1: double binWidth = sampleRate / fftSize;
Chris@1: int crossoverBin = (int)(2 / (Math.pow(2, 1/12.0) - 1));
Chris@1: int crossoverMidi = (int)Math.round(Math.log(crossoverBin*binWidth/440)/
Chris@1: Math.log(2) * 12 + 69);
Chris@1: // freq = 440 * Math.pow(2, (midi-69)/12.0) / binWidth;
Chris@1: int i = 0;
Chris@1: while (i <= crossoverBin)
Chris@1: freqMap[i++] = i;
Chris@1: while (i <= fftSize/2) {
Chris@1: double midi = Math.log(i*binWidth/440) / Math.log(2) * 12 + 69;
Chris@1: if (midi > 127)
Chris@1: midi = 127;
Chris@1: freqMap[i++] = crossoverBin + (int)Math.round(midi) - crossoverMidi;
Chris@1: }
Chris@1: freqMapSize = freqMap[i-1] + 1;
Chris@1: } // makeFreqMap()
Chris@1:
Chris@1: /** Calculates the weighted phase deviation onset detection function.
Chris@1: * Not used.
Chris@1: * TODO: Test the change to WPD fn */
Chris@1: protected void weightedPhaseDeviation() {
Chris@1: if (frameCount < 2)
Chris@1: phaseDeviation[frameCount] = 0;
Chris@1: else {
Chris@1: for (int i = 0; i < fftSize; i++) {
Chris@1: double pd = imBuffer[i] - 2 * prevPhase[i] + prevPrevPhase[i];
Chris@1: double pd1 = Math.abs(Math.IEEEremainder(pd, 2 * Math.PI));
Chris@1: phaseDeviation[frameCount] += pd1 * reBuffer[i];
Chris@1: // System.err.printf("%7.3f %7.3f\n", pd/Math.PI, pd1/Math.PI);
Chris@1: }
Chris@1: }
Chris@1: phaseDeviation[frameCount] /= fftSize * Math.PI;
Chris@1: double[] tmp = prevPrevPhase;
Chris@1: prevPrevPhase = prevPhase;
Chris@1: prevPhase = imBuffer;
Chris@1: imBuffer = tmp;
Chris@1: } // weightedPhaseDeviation()
Chris@1:
Chris@1: /** Reads a frame of input data, averages the channels to mono, scales
Chris@1: * to a maximum possible absolute value of 1, and stores the audio data
Chris@1: * in a circular input buffer.
Chris@1: * @return true if a frame (or part of a frame, if it is the final frame)
Chris@1: * is read. If a complete frame cannot be read, the InputStream is set
Chris@1: * to null.
Chris@1: */
Chris@1: public boolean getFrame() {
Chris@1: if (pcmInputStream == null)
Chris@1: return false;
Chris@1: try {
Chris@1: int bytesRead = (int) pcmInputStream.read(inputBuffer);
Chris@1: if ((audioOut != null) && (bytesRead > 0))
Chris@1: if (audioOut.write(inputBuffer, 0, bytesRead) != bytesRead)
Chris@1: System.err.println("Error writing to audio device");
Chris@1: if (bytesRead < inputBuffer.length) {
Chris@1: if (!silent)
Chris@1: System.err.println("End of input: " + audioFileName);
Chris@1: closeStreams();
Chris@1: return false;
Chris@1: }
Chris@1: } catch (IOException e) {
Chris@1: e.printStackTrace();
Chris@1: closeStreams();
Chris@1: return false;
Chris@1: }
Chris@1: frameRMS = 0;
Chris@1: double sample;
Chris@1: switch(channels) {
Chris@1: case 1:
Chris@1: for (int i = 0; i < inputBuffer.length; i += 2) {
Chris@1: sample = ((inputBuffer[i+1]<<8) |
Chris@1: (inputBuffer[i]&0xff)) / 32768.0;
Chris@1: frameRMS += sample * sample;
Chris@1: circBuffer[cbIndex++] = sample;
Chris@1: if (cbIndex == fftSize)
Chris@1: cbIndex = 0;
Chris@1: }
Chris@1: break;
Chris@1: case 2: // saves ~0.1% of RT (total input overhead ~0.4%) :)
Chris@1: for (int i = 0; i < inputBuffer.length; i += 4) {
Chris@1: sample = (((inputBuffer[i+1]<<8) | (inputBuffer[i]&0xff)) +
Chris@1: ((inputBuffer[i+3]<<8) | (inputBuffer[i+2]&0xff)))
Chris@1: / 65536.0;
Chris@1: frameRMS += sample * sample;
Chris@1: circBuffer[cbIndex++] = sample;
Chris@1: if (cbIndex == fftSize)
Chris@1: cbIndex = 0;
Chris@1: }
Chris@1: break;
Chris@1: default:
Chris@1: for (int i = 0; i < inputBuffer.length; ) {
Chris@1: sample = 0;
Chris@1: for (int j = 0; j < channels; j++, i+=2)
Chris@1: sample += (inputBuffer[i+1]<<8) | (inputBuffer[i]&0xff);
Chris@1: sample /= 32768.0 * channels;
Chris@1: frameRMS += sample * sample;
Chris@1: circBuffer[cbIndex++] = sample;
Chris@1: if (cbIndex == fftSize)
Chris@1: cbIndex = 0;
Chris@1: }
Chris@1: }
Chris@1: frameRMS = Math.sqrt(frameRMS / inputBuffer.length * 2 * channels);
Chris@1: return true;
Chris@1: } // getFrame()
Chris@1:
Chris@1: /** Processes a frame of audio data by first computing the STFT with a
Chris@1: * Hamming window, then mapping the frequency bins into a part-linear
Chris@1: * part-logarithmic array, then computing the spectral flux
Chris@1: * then (optionally) normalising and calculating onsets.
Chris@1: */
Chris@1: protected void processFrame() {
Chris@1: if (getFrame()) {
Chris@1: for (int i = 0; i < fftSize; i++) {
Chris@1: reBuffer[i] = window[i] * circBuffer[cbIndex];
Chris@1: if (++cbIndex == fftSize)
Chris@1: cbIndex = 0;
Chris@1: }
Chris@1: Arrays.fill(imBuffer, 0);
Chris@1: FFT.magnitudePhaseFFT(reBuffer, imBuffer);
Chris@1: Arrays.fill(newFrame, 0);
Chris@1: double flux = 0;
Chris@1: for (int i = 0; i <= fftSize/2; i++) {
Chris@1: if (reBuffer[i] > prevFrame[i])
Chris@1: flux += reBuffer[i] - prevFrame[i];
Chris@1: newFrame[freqMap[i]] += reBuffer[i];
Chris@1: }
Chris@1: spectralFlux[frameCount] = flux;
Chris@1: for (int i = 0; i < freqMapSize; i++)
Chris@1: frames[frameCount][i] = newFrame[i];
Chris@1: int index = cbIndex - (fftSize - hopSize);
Chris@1: if (index < 0)
Chris@1: index += fftSize;
Chris@1: int sz = (fftSize - hopSize) / energyOversampleFactor;
Chris@1: for (int j = 0; j < energyOversampleFactor; j++) {
Chris@1: double newEnergy = 0;
Chris@1: for (int i = 0; i < sz; i++) {
Chris@1: newEnergy += circBuffer[index] * circBuffer[index];
Chris@1: if (++index == fftSize)
Chris@1: index = 0;
Chris@1: }
Chris@1: energy[frameCount * energyOversampleFactor + j] =
Chris@1: newEnergy / sz <= 1e-6? 0: Math.log(newEnergy / sz) + 13.816;
Chris@1: }
Chris@1: double decay = frameCount >= 200? 0.99:
Chris@1: (frameCount < 100? 0: (frameCount - 100) / 100.0);
Chris@1: if (ltAverage == 0)
Chris@1: ltAverage = frameRMS;
Chris@1: else
Chris@1: ltAverage = ltAverage * decay + frameRMS * (1.0 - decay);
Chris@1: if (frameRMS <= silenceThreshold)
Chris@1: for (int i = 0; i < freqMapSize; i++)
Chris@1: frames[frameCount][i] = 0;
Chris@1: else {
Chris@1: if (normaliseMode == 1)
Chris@1: for (int i = 0; i < freqMapSize; i++)
Chris@1: frames[frameCount][i] /= frameRMS;
Chris@1: else if (normaliseMode == 2)
Chris@1: for (int i = 0; i < freqMapSize; i++)
Chris@1: frames[frameCount][i] /= ltAverage;
Chris@1: for (int i = 0; i < freqMapSize; i++) {
Chris@1: frames[frameCount][i] = Math.log(frames[frameCount][i]) + rangeThreshold;
Chris@1: if (frames[frameCount][i] < 0)
Chris@1: frames[frameCount][i] = 0;
Chris@1: }
Chris@1: }
Chris@1: // weightedPhaseDeviation();
Chris@1: // if (debug)
Chris@1: // System.err.printf("PhaseDev: t=%7.3f phDev=%7.3f RMS=%7.3f\n",
Chris@1: // frameCount * hopTime,
Chris@1: // phaseDeviation[frameCount],
Chris@1: // frameRMS);
Chris@1: double[] tmp = prevFrame;
Chris@1: prevFrame = reBuffer;
Chris@1: reBuffer = tmp;
Chris@1: frameCount++;
Chris@1: if ((frameCount % 100) == 0) {
Chris@1: if (!silent) {
Chris@1: System.err.printf("Progress: %1d %5.3f %5.3f\n",
Chris@1: frameCount, frameRMS, ltAverage);
Chris@1: Profile.report();
Chris@1: }
Chris@1: if ((progressCallback != null) && (totalFrames > 0))
Chris@1: progressCallback.setFraction((double)frameCount/totalFrames);
Chris@1: }
Chris@1: }
Chris@1: } // processFrame()
Chris@1:
Chris@1: /** Processes a complete file of audio data. */
Chris@1: public void processFile() {
Chris@1: while (pcmInputStream != null) {
Chris@1: // Profile.start(0);
Chris@1: processFrame();
Chris@1: // Profile.log(0);
Chris@1: if (Thread.currentThread().isInterrupted()) {
Chris@1: System.err.println("info: INTERRUPTED in processFile()");
Chris@1: return;
Chris@1: }
Chris@1: }
Chris@1:
Chris@1: // double[] x1 = new double[phaseDeviation.length];
Chris@1: // for (int i = 0; i < x1.length; i++) {
Chris@1: // x1[i] = i * hopTime;
Chris@1: // phaseDeviation[i] = (phaseDeviation[i] - 0.4) * 100;
Chris@1: // }
Chris@1: // double[] x2 = new double[energy.length];
Chris@1: // for (int i = 0; i < x2.length; i++)
Chris@1: // x2[i] = i * hopTime / energyOversampleFactor;
Chris@1: // // plot.clear();
Chris@1: // plot.addPlot(x1, phaseDeviation, Color.green, 7);
Chris@1: // plot.addPlot(x2, energy, Color.red, 7);
Chris@1: // plot.setTitle("Test phase deviation");
Chris@1: // plot.fitAxes();
Chris@1:
Chris@1: // double[] slope = new double[energy.length];
Chris@1: // double hop = hopTime / energyOversampleFactor;
Chris@1: // Peaks.getSlope(energy, hop, 15, slope);
Chris@1: // LinkedList peaks = Peaks.findPeaks(slope, (int)Math.round(0.06 / hop), 10);
Chris@1:
Chris@1: double hop = hopTime;
Chris@1: Peaks.normalise(spectralFlux);
Chris@1: LinkedList peaks = Peaks.findPeaks(spectralFlux, (int)Math.round(0.06 / hop), 0.35, 0.84, true);
Chris@1: onsets = new double[peaks.size()];
Chris@1: double[] y2 = new double[onsets.length];
Chris@1: Iterator it = peaks.iterator();
Chris@1: onsetList = new EventList();
Chris@1: double minSalience = Peaks.min(spectralFlux);
Chris@1: for (int i = 0; i < onsets.length; i++) {
Chris@1: int index = it.next();
Chris@1: onsets[i] = index * hop;
Chris@1: y2[i] = spectralFlux[index];
Chris@1: Event e = BeatTrackDisplay.newBeat(onsets[i], 0);
Chris@1: // if (debug)
Chris@1: // System.err.printf("Onset: %8.3f %8.3f %8.3f\n",
Chris@1: // onsets[i], energy[index], slope[index]);
Chris@1: // e.salience = slope[index]; // or combination of energy + slope??
Chris@1: // Note that salience must be non-negative or the beat tracking system fails!
Chris@1: e.salience = spectralFlux[index] - minSalience;
Chris@1: onsetList.add(e);
Chris@1: }
Chris@1: if (progressCallback != null)
Chris@1: progressCallback.setFraction(1.0);
Chris@1: if (doOnsetPlot) {
Chris@1: double[] x1 = new double[spectralFlux.length];
Chris@1: for (int i = 0; i < x1.length; i++)
Chris@1: x1[i] = i * hopTime;
Chris@1: plot.addPlot(x1, spectralFlux, Color.red, 4);
Chris@1: plot.addPlot(onsets, y2, Color.green, 3);
Chris@1: plot.setTitle("Spectral flux and onsets");
Chris@1: plot.fitAxes();
Chris@1: }
Chris@1: if (debug) {
Chris@1: System.err.printf("Onsets: %d\nContinue? ", onsets.length);
Chris@1: readLine();
Chris@1: }
Chris@1: } // processFile()
Chris@1:
Chris@1: /** Reads a text file containing a list of whitespace-separated feature values.
Chris@1: * Created for paper submitted to ICASSP'07.
Chris@1: * @param fileName File containing the data
Chris@1: * @return An array containing the feature values
Chris@1: */
Chris@1: public static double[] getFeatures(String fileName) {
Chris@1: ArrayList l = new ArrayList();
Chris@1: try {
Chris@1: BufferedReader b = new BufferedReader(new FileReader(fileName));
Chris@1: while (true) {
Chris@1: String s = b.readLine();
Chris@1: if (s == null)
Chris@1: break;
Chris@1: int start = 0;
Chris@1: while (start < s.length()) {
Chris@1: int len = s.substring(start).indexOf(' ');
Chris@1: String t = null;
Chris@1: if (len < 0)
Chris@1: t = s.substring(start);
Chris@1: else if (len > 0) {
Chris@1: t = s.substring(start, start + len);
Chris@1: }
Chris@1: if (t != null)
Chris@1: try {
Chris@1: l.add(Double.parseDouble(t));
Chris@1: } catch (NumberFormatException e) {
Chris@1: System.err.println(e);
Chris@1: if (l.size() == 0)
Chris@1: l.add(new Double(0));
Chris@1: else
Chris@1: l.add(new Double(l.get(l.size()-1)));
Chris@1: }
Chris@1: start += len + 1;
Chris@1: if (len < 0)
Chris@1: break;
Chris@1: }
Chris@1: }
Chris@1: double[] features = new double[l.size()];
Chris@1: Iterator it = l.iterator();
Chris@1: for (int i = 0; it.hasNext(); i++)
Chris@1: features[i] = it.next().doubleValue();
Chris@1: return features;
Chris@1: } catch (FileNotFoundException e) {
Chris@1: e.printStackTrace();
Chris@1: return null;
Chris@1: } catch (IOException e) {
Chris@1: e.printStackTrace();
Chris@1: return null;
Chris@1: } catch (NumberFormatException e) {
Chris@1: e.printStackTrace();
Chris@1: return null;
Chris@1: }
Chris@1: } // getFeatures()
Chris@1:
Chris@1: /** Reads a file of feature values, treated as an onset detection function,
Chris@1: * and finds peaks, which are stored in onsetList
and onsets
.
Chris@1: * @param fileName The file of feature values
Chris@1: * @param hopTime The spacing of feature values in time
Chris@1: */
Chris@1: public void processFeatures(String fileName, double hopTime) {
Chris@1: double hop = hopTime;
Chris@1: double[] features = getFeatures(fileName);
Chris@1: Peaks.normalise(features);
Chris@1: LinkedList peaks = Peaks.findPeaks(features, (int)Math.round(0.06 / hop), 0.35, 0.84, true);
Chris@1: onsets = new double[peaks.size()];
Chris@1: double[] y2 = new double[onsets.length];
Chris@1: Iterator it = peaks.iterator();
Chris@1: onsetList = new EventList();
Chris@1: double minSalience = Peaks.min(features);
Chris@1: for (int i = 0; i < onsets.length; i++) {
Chris@1: int index = it.next();
Chris@1: onsets[i] = index * hop;
Chris@1: y2[i] = features[index];
Chris@1: Event e = BeatTrackDisplay.newBeat(onsets[i], 0);
Chris@1: e.salience = features[index] - minSalience;
Chris@1: onsetList.add(e);
Chris@1: }
Chris@1: } // processFeatures()
Chris@1:
Chris@1: /** Copies output of audio processing to the display panel. */
Chris@1: public void setDisplay(BeatTrackDisplay btd) {
Chris@1: int energy2[] = new int[totalFrames*energyOversampleFactor];
Chris@1: double time[] = new double[totalFrames*energyOversampleFactor];
Chris@1: for (int i = 0; i < totalFrames*energyOversampleFactor; i++) {
Chris@1: energy2[i] = (int) (energy[i] * 4 * energyOversampleFactor);
Chris@1: time[i] = i * hopTime / energyOversampleFactor;
Chris@1: }
Chris@1: btd.setMagnitudes(energy2);
Chris@1: btd.setEnvTimes(time);
Chris@1: btd.setSpectro(frames, totalFrames, hopTime, 0);//fftTime/hopTime);
Chris@1: btd.setOnsets(onsets);
Chris@1: btd.setOnsetList(onsetList);
Chris@1: } // setDisplay()
Chris@1:
Chris@1: } // class AudioProcessor
Chris@1:
Chris@1:
Chris@1: #endif