From 299af9442dc3bb591a8d41f6cc4600b21abbc3e0 Mon Sep 17 00:00:00 2001 From: Dave Griffiths Date: Wed, 8 Jul 2015 15:06:08 +0100 Subject: [PATCH] working with fft --- samplebrain/src/brain.cpp | 75 +++++++++++++++++++++++---------- samplebrain/src/brain.h | 10 ++--- samplebrain/src/brain_block.cpp | 52 +++++++++++++---------- samplebrain/src/brain_block.h | 2 +- samplebrain/src/libmfcc.cpp | 36 ++++++++-------- samplebrain/src/libmfcc.h | 12 +++--- samplebrain/src/main.cpp | 25 ++++++++++- 7 files changed, 135 insertions(+), 77 deletions(-) diff --git a/samplebrain/src/brain.cpp b/samplebrain/src/brain.cpp index 9c18f4d..629c6cf 100644 --- a/samplebrain/src/brain.cpp +++ b/samplebrain/src/brain.cpp @@ -16,29 +16,45 @@ sample brain::load_sound(std::string filename) { sfinfo.format=0; SNDFILE* f=sf_open(filename.c_str(), SFM_READ, &sfinfo); sample s(sfinfo.frames); - sf_read_float(f, s.get_non_const_buffer(), s.get_length()); + sf_readf_float(f, s.get_non_const_buffer(), s.get_length()); sf_close(f); m_samples.push_back(s); return s; } +void save_sample(const string &filename, const sample s) { + SF_INFO sfinfo; + sfinfo.format=SF_FORMAT_WAV | SF_FORMAT_FLOAT; + sfinfo.frames=s.get_length(); + sfinfo.samplerate=44100; + sfinfo.channels=1; + sfinfo.sections=1; + sfinfo.seekable=0; + SNDFILE* f=sf_open(filename.c_str(), SFM_WRITE, &sfinfo); + if (!f) cerr<<"couldn't open "<::iterator i=m_samples.begin(); i!=m_samples.end(); ++i) { - chop_and_add(*i, block_size, overlap); + chop_and_add(*i, block_size, overlap, ditchpcm); } } -void brain::chop_and_add(const sample &s, u32 block_size, u32 overlap) { +void brain::chop_and_add(const sample &s, u32 block_size, u32 overlap, bool ditchpcm) { u32 pos=0; while (pos+block_size-1::iterator i=m_blocks.begin(); i!=m_blocks.end(); ++i) { + cerr< m_blocks; vector m_samples; diff --git a/samplebrain/src/brain_block.cpp b/samplebrain/src/brain_block.cpp index 74cf815..ea7cfa4 100644 --- a/samplebrain/src/brain_block.cpp +++ b/samplebrain/src/brain_block.cpp @@ -8,37 +8,38 @@ using namespace spiralcore; FFT *brain_block::m_fftw; -brain_block::brain_block(const string &filename, const sample &pcm, u32 rate) : +static const int MFCC_FILTERS=48; + +void enveloper(sample &s, u32 start, u32 end) { + for(u32 i=0; im_in[i] = m_pcm[i]; - } + enveloper(m_pcm,50,50); - m_fftw->raw_impulse2freq(); + m_fftw->impulse2freq(m_pcm.get_non_const_buffer(), + m_fft.get_non_const_buffer()); - double *spectrum = new double[m_block_size]; + if (m_block_size>30) m_fft.crop_to(30); + if (ditchpcm) m_pcm.clear(); - for (u32 i=0; im_spectrum[i][0]; - // convert from complex to double for mfcc calc - spectrum[i] = m_fftw->m_spectrum[i][0]; - } - - for (u32 i=0; i<13; i++) { - m_mfcc[i] = GetCoefficient(spectrum, rate, 48, m_block_size, i); - } - - delete[] spectrum; +// for (u32 i=0; i= 0 && boundary < prevCenterFrequency) { @@ -119,9 +119,9 @@ double GetFilterParameter(unsigned int samplingRate, unsigned int binSize, unsig * Compute the band-dependent magnitude factor for the given filter band (Eq. 3) * Used for internal computation only - not the be called directly */ -double GetMagnitudeFactor(unsigned int filterBand) +mfcc_real GetMagnitudeFactor(unsigned int filterBand) { - double magnitudeFactor = 0.0f; + mfcc_real magnitudeFactor = 0.0f; if(filterBand >= 1 && filterBand <= 14) { @@ -141,10 +141,10 @@ double GetMagnitudeFactor(unsigned int filterBand) * center frequencies are equally spaced on the mel scale * Used for internal computation only - not the be called directly */ -double GetCenterFrequency(unsigned int filterBand) +mfcc_real GetCenterFrequency(unsigned int filterBand) { - double centerFrequency = 0.0f; - double exponent; + mfcc_real centerFrequency = 0.0f; + mfcc_real exponent; if(filterBand == 0) { diff --git a/samplebrain/src/libmfcc.h b/samplebrain/src/libmfcc.h index a5ecc42..ec7615c 100644 --- a/samplebrain/src/libmfcc.h +++ b/samplebrain/src/libmfcc.h @@ -8,19 +8,21 @@ #pragma once +typedef float mfcc_real; + #define PI 3.14159265358979323846264338327 // Returns the specified (mth) MFCC -double GetCoefficient(double* spectralData, unsigned int samplingRate, unsigned int NumFilters, unsigned int binSize, unsigned int m); +mfcc_real GetCoefficient(mfcc_real* spectralData, unsigned int samplingRate, unsigned int NumFilters, unsigned int binSize, unsigned int m); // Compute the normalization factor (For internal computation only - not to be called directly) -double NormalizationFactor(int NumFilters, int m); +mfcc_real NormalizationFactor(int NumFilters, int m); // Compute the filter parameter for the specified frequency and filter bands (For internal computation only - not the be called directly) -double GetFilterParameter(unsigned int samplingRate, unsigned int binSize, unsigned int frequencyBand, unsigned int filterBand); +mfcc_real GetFilterParameter(unsigned int samplingRate, unsigned int binSize, unsigned int frequencyBand, unsigned int filterBand); // Compute the band-dependent magnitude factor for the given filter band (For internal computation only - not the be called directly) -double GetMagnitudeFactor(unsigned int filterBand); +mfcc_real GetMagnitudeFactor(unsigned int filterBand); // Compute the center frequency (fc) of the specified filter band (l) (For internal computation only - not the be called directly) -double GetCenterFrequency(unsigned int filterBand); +mfcc_real GetCenterFrequency(unsigned int filterBand); diff --git a/samplebrain/src/main.cpp b/samplebrain/src/main.cpp index f1dba0d..08471d5 100644 --- a/samplebrain/src/main.cpp +++ b/samplebrain/src/main.cpp @@ -25,14 +25,35 @@ using namespace std; -int main(int argc, char *argv[]) -{ +void unit_test() { cerr<<"testing brain_block"<