
org.robokind.impl.audio.visualization.SpectrogramImage Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2011 Hanson Robokind LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.robokind.impl.audio.visualization;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.AudioFormat;
import org.apache.commons.math.complex.Complex;
import org.robokind.api.audio.processing.FFTWindow;
import org.robokind.api.audio.processing.HammingWindow;
import org.robokind.api.audio.processing.MeanCalculator;
import org.robokind.api.audio.processing.SampleProcessor;
import org.robokind.api.audio.processing.SampleProcessor.ProcessorListener;
import org.robokind.api.audio.processing.WavProcessor;
import org.robokind.impl.audio.processing.FFTBuffer;
/**
*
* @author Matthew Stevenson
*/
public class SpectrogramImage {
private double myNormalize;
private int myOverlapCount;
private int myImageColumnIndex;
private int myFFTLen;
private int myFFTHalfLen;
private int myChannelCount;
private BufferedImage myChannelImages[];
private WavProcessor myWavProc;
private FFTBuffer myFFT;
private MeanCalculator myMean;
private FFTWindow myWindow;
private SpectrogramProcessor myProc;
//private double myLogScaleFactor;
//private double myLogStep;
public SpectrogramImage(WavProcessor wavProc, int fftLen,
int channels, double normalizationFactor){//, double logScale){
myWavProc = wavProc;
myWavProc.setSamplesBufferSize(fftLen);
myFFTLen = fftLen;
myFFTHalfLen = fftLen/2;
myImageColumnIndex = 0;
myNormalize = normalizationFactor;
myOverlapCount = 1;
AudioFormat format = myWavProc.getFormat();
if(format == null){
throw new NullPointerException();
}
//myChannelCount = Math.min(channels,format.getChannels());
myChannelCount = format.getChannels();
myChannelImages = new BufferedImage[myChannelCount];
myProc = new SpectrogramProcessor();
myMean = new MeanCalculator(myChannelCount);
initImage();
initFFTBuffer();
//myLogScaleFactor = logScale;
//myLogStep = Math.log(myFFTHalfLen/myLogScaleFactor)/Math.log(10.0);
//myLogStep /= myFFTHalfLen;
}
public MeanCalculator getMeanCalculator(){
return myMean;
}
private void initImage(){
long frame = myWavProc.getFrameCount();
int width = (int)((frame*myOverlapCount)/myFFTLen)+1;
int height = myFFTHalfLen;
for(int c=0; c> 16) & 0xff;
int aG = (a >> 8) & 0xff;
int aB = a & 0xff;
int bR = (b >> 16) & 0xff;
int bG = (b >> 8) & 0xff;
int bB = b & 0xff;
int cR = avg(aR, bR, p);
int cG = avg(aG, bG, p);
int cB = avg(aB, bB, p);
return getRGB(cR, cG, cB);
}
private int avg(double a, double b, double p){
double avg = a*p + b*(1-p);
return (int)avg;
}*/
private class SpectrogramProcessor implements SampleProcessor{
private List myListeners;
public SpectrogramProcessor(){
myListeners = new ArrayList(3);
}
@Override
public void processSamples(double[][] samples, int frame, int total) {
myFFT.writeData(samples);
Complex[][] data = myFFT.getData();
for(int c=0; c
© 2015 - 2025 Weber Informatics LLC | Privacy Policy