public void TestSpectrogram() { // harmor_LQ.bmp = 1645 (width) x 255 (height) 32 bit // harmor_HQ.bmp = 1645 (width) x 511 (height) 32 bit // test variables var audioSystem = BassProxy.Instance; // 0. Get Audio Data float[] audioSamples = BassProxy.ReadMonoFromFile(WAVE_INPUT_FILEPATH, SAMPLING_RATE); /* * // generate spectrogram * Bitmap spectroBW = AudioAnalyzer.GetSpectrogramImage(audioSamples, -1, 591, SAMPLING_RATE, WINDOW_SIZE, OVERLAP, ColorUtils.ColorPaletteType.BLACK_AND_WHITE, true); * spectroBW.Save(@"spectrogram-log-blackwhite.png"); * * Bitmap spectro = AudioAnalyzer.GetSpectrogramImage(audioSamples, -1, 591, SAMPLING_RATE, WINDOW_SIZE, OVERLAP, ColorUtils.ColorPaletteType.PHOTOSOUNDER, true); * spectro.Save(@"spectrogram-log-photosounder.png"); * * Bitmap spectro2 = AudioAnalyzer.GetSpectrogramImage(audioSamples, -1, 591, SAMPLING_RATE, WINDOW_SIZE, OVERLAP, ColorUtils.ColorPaletteType.REW, true); * spectro2.Save(@"spectrogram-log-rew.png"); * * Bitmap spectro3 = AudioAnalyzer.GetSpectrogramImage(audioSamples, -1, 591, SAMPLING_RATE, WINDOW_SIZE, OVERLAP, ColorUtils.ColorPaletteType.SOX, true); * spectro3.Save(@"spectrogram-log-sox.png"); * * Bitmap spectro4 = AudioAnalyzer.GetSpectrogramImage(audioSamples, -1, 591, SAMPLING_RATE, WINDOW_SIZE, OVERLAP, ColorUtils.ColorPaletteType.MATLAB, true); * spectro4.Save(@"spectrogram-log-matlab.png"); */ //double minFrequency = 27.5; //double maxFrequency = SAMPLING_RATE / 2; //int logBins = 512; //var logFrequenciesIndex = new int[1]; //var logFrequencies = new float[1]; // find the time int wanted_width = 1645; //int width = (audioSamples.Length - WINDOW_SIZE)/ OVERLAP; int OVERLAP = (int)((double)(audioSamples.Length - WINDOW_SIZE) / (double)wanted_width); // int OVERLAP = WINDOW_SIZE/2; int numberOfSamples = audioSamples.Length; double seconds = numberOfSamples / SAMPLING_RATE; float[][] spectrogram = AudioAnalyzer.CreateSpectrogramLomont(audioSamples, WINDOW_SIZE, OVERLAP); int width = spectrogram.Length + 2 * 60; int height = spectrogram[0].Length + 2 * 40; Bitmap bmp1 = AudioAnalyzer.GetSpectrogramImage(spectrogram, width, height, seconds * 1000, SAMPLING_RATE, ColorUtils.ColorPaletteType.BLACK_AND_WHITE, false, null, null); bmp1.Save(@"spectrogram-blackwhite.png"); Bitmap bmp2 = AudioAnalyzer.GetSpectrogramImage(audioSamples, width, height, SAMPLING_RATE, WINDOW_SIZE, OVERLAP, ColorUtils.ColorPaletteType.BLACK_AND_WHITE, true); bmp2.Save(@"spectrogram-blackwhite-2.png"); }
public static void Main(string[] args) { /* * int nFFT = 1024; * int samplerate = 44100; * int length = samplerate * 10; // 10 sec * * double freq1, freq2; * int i1, i2; * for (int i = 0; i < nFFT + 1; i++) { * freq1 = MathUtils.Index2Freq(i, samplerate, nFFT); * freq2 = MathUtils.IndexToFreq(i, samplerate, nFFT); * i1 = MathUtils.Freq2Index(freq1, samplerate, nFFT); * i2 = MathUtils.FreqToIndex((float)freq2, samplerate, nFFT); * } */ // http://www.music.mcgill.ca/~gary/307/week5/additive.html //SaveColorPaletteBar("c:\\rew-colorbar-generated.png", "c:\\rew-colorbar-generated.csv", ColorPaletteType.REWColorPalette); /* * List<Color> rew_hsb_gradients = ColorUtils.GetHSBColorGradients(256, ColorUtils.ColorPaletteType.REW); * ColorUtils.SaveColorGradients("c:\\rew-hsb-gradients.png", rew_hsb_gradients, 40); * List<Color> rew_hsl_gradients = ColorUtils.GetHSLColorGradients(256, ColorUtils.ColorPaletteType.REW); * ColorUtils.SaveColorGradients("c:\\rew-hsl-gradients.png", rew_hsl_gradients, 40); * * List<Color> sox_hsb_gradients = ColorUtils.GetHSBColorGradients(256, ColorUtils.ColorPaletteType.SOX); * ColorUtils.SaveColorGradients("c:\\sox-hsb-gradients.png", sox_hsb_gradients, 40); * List<Color> sox_hsl_gradients = ColorUtils.GetHSLColorGradients(256, ColorUtils.ColorPaletteType.SOX); * ColorUtils.SaveColorGradients("c:\\sox-hsl-gradients.png", sox_hsl_gradients, 40); * * List<Color> photosounder_hsb_gradients = ColorUtils.GetHSBColorGradients(256, ColorUtils.ColorPaletteType.PHOTOSOUNDER); * ColorUtils.SaveColorGradients("c:\\photosounder_hsb_gradients.png", photosounder_hsb_gradients, 40); * List<Color> photosounder_hsl_gradients = ColorUtils.GetHSLColorGradients(256, ColorUtils.ColorPaletteType.PHOTOSOUNDER); * ColorUtils.SaveColorGradients("c:\\photosounder_hsl_gradients.png", photosounder_hsl_gradients, 40); * List<Color> photosounder_rgb_gradients = ColorUtils.GetRGBColorGradients(255, ColorUtils.ColorPaletteType.PHOTOSOUNDER); * ColorUtils.SaveColorGradients("c:\\photosounder_rgb_gradients.png", photosounder_rgb_gradients, 40); * * List<Color> grey_hsb_gradients = ColorUtils.GetHSBColorGradients(256, ColorUtils.ColorPaletteType.BLACK_AND_WHITE); * ColorUtils.SaveColorGradients("c:\\grey-hsb-gradients.png", grey_hsb_gradients, 40); */ /* * ReadColorPaletteBar(@"C:\Users\perivar.nerseth\SkyDrive\Temp\sox_colorbar.png", "c:\\sox_colorbar.csv"); * ReadColorPaletteBar(@"C:\Users\perivar.nerseth\SkyDrive\Temp\soundforge_colorbar.png", "c:\\soundforge_colorbar.csv"); * ReadColorPaletteBar(@"C:\Users\perivar.nerseth\SkyDrive\Temp\rew_colorbar.png", "c:\\rew_colorbar.csv"); * ReadColorPaletteBar(@"C:\Users\perivar.nerseth\SkyDrive\Temp\sox_colorbar.png", "c:\\sox_colorbar.csv"); * ReadColorPaletteBar(@"C:\Users\perivar.nerseth\SkyDrive\Temp\thermal_colorbar.png", "c:\\thermal_colorbar.csv"); * ReadColorPaletteBar(@"C:\rew-gradients.png", "c:\\rew-gradients.csv"); */ //String fileName = @"C:\Users\perivar.nerseth\Music\Sleep Away.mp3"; //String fileName = @"C:\Users\perivar.nerseth\Music\Sleep Away32f.wav"; String fileName = @"C:\Users\perivar.nerseth\Music\Sleep Away16.wav"; //String fileName = @"C:\Users\perivar.nerseth\Music\Maid with the Flaxen Hair.mp3"; //String fileName = @"G:\Cubase and Nuendo Projects\Music To Copy Learn\Britney Spears - Hold It Against Me\02 Hold It Against Me (Instrumental) 1.mp3"; Console.WriteLine("Analyzer starting ..."); RepositoryGateway repositoryGateway = new RepositoryGateway(); FingerprintManager manager = new FingerprintManager(); // VB6 FFT double sampleRate = 44100; // 44100, default 5512 int fftWindowsSize = 16384; //32768 16384 8192 4096 2048, default 256*8 (2048) to 256*128 (32768), reccomended: 256*64 = 16384 int secondsToSample = 25; //25, 15; int fftOverlap = (int)(sampleRate * secondsToSample / 1280); // 32768:990, 16384:990, 8192:990, 4096:990 //float fftOverlapPercentage = 94.0f; // 99.0f number between 0 and 100 //float[] wavDataVB6 = repositoryGateway._proxy.ReadMonoFromFile(fileName, (int) sampleRate, secondsToSample*1000, 20*1000 ); //float[] wavDataVB6 = repositoryGateway._proxy.ReadMonoFromFile(fileName, (int) sampleRate, secondsToSample*1000, 0); //MathUtils.NormalizeInPlace(wavDataVB6); //Export.exportCSV(@"c:\bass.csv", wavDataVB6); float[] wavDataNaudio = AudioUtilsNAudio.ReadMonoFromFile(fileName, (int)sampleRate, secondsToSample * 1000, 0); //float[] wavDataNaudio = AudioUtilsNAudio.ReadMonoFromFile(fileName, (int) sampleRate, 0, 0); /* * float[] wavDataNaudio = new float[(int) (sampleRate*secondsToSample)]; * BasicOscillatorProvider basic = new BasicOscillatorProvider(); * * int length = (int)(sampleRate*3); * int offset = 0; * * basic.Amplitude = MathUtils.DecibelToAmplitude(-80); * basic.SetFrequency(220); * basic.SetOscWaveshape(BasicOscillatorProvider.WAVESHAPE.SINE); * offset += basic.Read(wavDataNaudio, offset, length); * * basic.Amplitude = MathUtils.DecibelToAmplitude(-60); * basic.SetFrequency(440); * basic.SetOscWaveshape(BasicOscillatorProvider.WAVESHAPE.SQUARE); * offset += basic.Read(wavDataNaudio, offset, length); * * basic.Amplitude = MathUtils.DecibelToAmplitude(-40); * basic.SetFrequency(880); * basic.SetOscWaveshape(BasicOscillatorProvider.WAVESHAPE.TRIANGLE); * offset += basic.Read(wavDataNaudio, offset, length); * * basic.Amplitude = MathUtils.DecibelToAmplitude(-20); * basic.SetFrequency(1760); * basic.SetOscWaveshape(BasicOscillatorProvider.WAVESHAPE.SAW); * offset += basic.Read(wavDataNaudio, offset, length); * * basic.Amplitude = MathUtils.DecibelToAmplitude(-10); * basic.SetFrequency(3520); * basic.SetOscWaveshape(BasicOscillatorProvider.WAVESHAPE.SINE); * offset += basic.Read(wavDataNaudio, offset, length); * * basic.Amplitude = MathUtils.DecibelToAmplitude(0); * basic.SetFrequency(1760); * basic.SetOscWaveshape(BasicOscillatorProvider.WAVESHAPE.SAW); * offset += basic.Read(wavDataNaudio, offset, length); * * AudioUtilsNAudio.WriteIEEE32WaveFileMono(@"c:\sines.wav", 44100, wavDataNaudio); */ //MathUtils.NormalizeInPlace(wavDataNaudio); //Export.exportCSV(@"c:\naudio.csv", wavDataNaudio); //VB6Spectrogram vb6Spect = new VB6Spectrogram(); //vb6Spect.ComputeColorPalette(); //float[][] vb6Spectrogram = vb6Spect.Compute(wavDataVB6, sampleRate, fftWindowsSize, fftOverlapPercentage); //Export.exportCSV (@"c:\VB6Spectrogram-full.csv", vb6Spectrogram); // Exocortex.DSP FFT /* * int numberOfSamples = wavDataNaudio.Length; * fftOverlapPercentage = fftOverlapPercentage / 100; * long ColSampleWidth = (long)(fftWindowsSize * (1 - fftOverlapPercentage)); * double fftOverlapSamples = fftWindowsSize * fftOverlapPercentage; * long NumCols = numberOfSamples / ColSampleWidth; * * int fftOverlap = (int)((numberOfSamples - fftWindowsSize) / NumCols); * int numberOfSegments = (numberOfSamples - fftWindowsSize)/fftOverlap; */ //System.Console.Out.WriteLine(String.Format("EXO: fftWindowsSize: {0}, Overlap samples: {1:n2}.", fftWindowsSize, fftOverlap )); //VIPSLib.Audio.WAVFile wavefile = new VIPSLib.Audio.WAVFile(); //wavefile.ReadFromFileToDouble(fileName.Substring(0, fileName.LastIndexOf(".")) + ".wav"); //RiffRead riff = new RiffRead(fileName.Substring(0, fileName.LastIndexOf(".")) + ".wav"); //riff.Process(); /* * VIPSLib.Audio.MFCC mfcclib = new VIPSLib.Audio.MFCC((float)sampleRate); * double[][] data = riff.SoundData; * double min; * double max; * MathUtils.ComputeMinAndMax(data, out min, out max); * double[][] mfcc = mfcclib.Process(MathUtils.FloatToDouble(wavDataVB6)); * * float fmin; * float fmax; * MathUtils.ComputeMinAndMax(wavDataVB6, out fmin, out fmax); */ //double[][] mfcc = mfcclib.Process(riff.SoundData[0]); //float[][] mfccFloats = MathUtils.DoubleToFloat(mfcc); // GENERATE SPECTROGRAM //Bitmap spectro = AudioAnalyzer.GetSpectrogramImage(wavDataNaudio, 1200, 600, sampleRate, fftWindowsSize, fftOverlap, ColorUtils.ColorPaletteType.PHOTOSOUNDER, true); //spectro.Save(@"c:\spectrogram-rew.png"); // The following lines replicate the BtnDrawSpectrumClick method // from Soundfingerprinting.SoundTools.DrawningTool fileName = @"C:\Users\perivar.nerseth\Music\Maid with the Flaxen Hair.mp3"; float[] wavDataBass = AudioUtilsBass.ReadMonoFromFile(fileName, 5512, 0, 0); float[][] data = AudioAnalyzer.CreateSpectrogramLomont(wavDataBass, 2048, 64); Bitmap image = AudioAnalyzer.GetSpectrogramImage(data, 1000, 800); image.Save(@"C:\Users\perivar.nerseth\Music\Maid with the Flaxen Hair_spectrum_2.jpg", ImageFormat.Jpeg); //float[][] logSpectrogram = manager.CreateLogSpectrogram(repositoryGateway._proxy, fileName, secondsToSample*1000, 0); //Bitmap logspectro = AudioAnalyzer.GetSpectrogramImage(logSpectrogram, 1200, 600, secondsToSample*1000, sampleRate, ColorUtils.ColorPaletteType.REW); //logspectro.Save(@"c:\spectrogram-log.png"); //Bitmap waveform = AudioAnalyzer.DrawWaveform(wavDataVB6, new Size (1200, 600), 0, 1, 0, sampleRate); //waveform.Save(@"c:\waveform.png"); //Bitmap waveform = AudioAnalyzer.DrawWaveform(wavDataNaudio, 1200, 600); //waveform.Save(@"c:\waveform.png"); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }