mirror of
https://gitlab.com/then-try-this/samplebrain.git
synced 2025-05-12 18:47:21 +00:00
added fftwack
This commit is contained in:
parent
dcfd92721a
commit
9b8bf39271
@ -68,12 +68,20 @@ void block::init_fft(u32 block_size)
|
||||
|
||||
#define FFT_BIAS 200
|
||||
|
||||
double block::compare(const block &other, float ratio) const {
|
||||
double block::compare(const block &other, float ratio, int fftwack) const {
|
||||
double mfcc_acc=0;
|
||||
double fft_acc=0;
|
||||
|
||||
/* s32 fft_start = fftwack-10;
|
||||
s32 fft_end = fftwack+10;
|
||||
if (fft_start<0) fft_start=0;
|
||||
if (fft_end>m_fft.get_length()) fft_end=m_fft.get_length();
|
||||
*/
|
||||
s32 fft_start = 0;
|
||||
s32 fft_end = m_fft.get_length();
|
||||
|
||||
if (ratio==0) {
|
||||
for (u32 i=0; i<m_fft.get_length(); ++i) {
|
||||
for (u32 i=fft_start; i<fft_end; ++i) {
|
||||
fft_acc+=(m_fft[i]-other.m_fft[i]) * (m_fft[i]-other.m_fft[i]);
|
||||
}
|
||||
return (fft_acc/(float)m_fft.get_length())*FFT_BIAS;
|
||||
@ -87,7 +95,7 @@ double block::compare(const block &other, float ratio) const {
|
||||
}
|
||||
|
||||
// calculate both
|
||||
for (u32 i=0; i<m_fft.get_length(); ++i) {
|
||||
for (u32 i=fft_start; i<fft_end; ++i) {
|
||||
fft_acc+=(m_fft[i]-other.m_fft[i]) * (m_fft[i]-other.m_fft[i]);
|
||||
}
|
||||
for (u32 i=0; i<MFCC_FILTERS; ++i) {
|
||||
@ -115,9 +123,9 @@ bool block::unit_test() {
|
||||
assert(bb.m_block_size==data.get_length());
|
||||
|
||||
block bb2("test",data,44100,0);
|
||||
assert(bb.compare(bb2,1)==0);
|
||||
assert(bb.compare(bb2,0)==0);
|
||||
assert(bb.compare(bb2,0.5)==0);
|
||||
assert(bb.compare(bb2,1,0)==0);
|
||||
assert(bb.compare(bb2,0,0)==0);
|
||||
assert(bb.compare(bb2,0.5,0)==0);
|
||||
|
||||
sample data2(200);
|
||||
for (u32 i=0; i<data.get_length(); i++) {
|
||||
@ -127,9 +135,9 @@ bool block::unit_test() {
|
||||
block cpy("test",data,100,4);
|
||||
{
|
||||
block bb3("test",data2,44100,4);
|
||||
assert(bb.compare(bb3,1)!=0);
|
||||
assert(bb.compare(bb3,0)!=0);
|
||||
assert(bb.compare(bb3,0.5)!=0);
|
||||
assert(bb.compare(bb3,1,0)!=0);
|
||||
assert(bb.compare(bb3,0,0)!=0);
|
||||
assert(bb.compare(bb3,0.5,0)!=0);
|
||||
cpy=bb3;
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@ public:
|
||||
block(const std::string &filename, const sample &pcm, u32 rate, u32 env, bool ditchpcm=false);
|
||||
|
||||
// returns distance based on ratio of fft-mfcc values
|
||||
double compare(const block &other, float ratio) const;
|
||||
double compare(const block &other, float ratio, int fftwack) const;
|
||||
|
||||
static void init_fft(u32 block_size);
|
||||
static bool unit_test();
|
||||
|
@ -68,12 +68,12 @@ const block &brain::get_block(u32 index) const {
|
||||
}
|
||||
|
||||
// returns index to block
|
||||
u32 brain::search(const block &target, float ratio) const {
|
||||
u32 brain::search(const block &target, float ratio, u32 fftwack) const {
|
||||
double closest = 999999999;
|
||||
u32 closest_index = 0;
|
||||
u32 index = 0;
|
||||
for (vector<block>::const_iterator i=m_blocks.begin(); i!=m_blocks.end(); ++i) {
|
||||
double diff = target.compare(*i,ratio);
|
||||
double diff = target.compare(*i,ratio,fftwack);
|
||||
if (diff<closest) {
|
||||
closest=diff;
|
||||
closest_index = index;
|
||||
@ -85,7 +85,7 @@ u32 brain::search(const block &target, float ratio) const {
|
||||
|
||||
// take another brain and rebuild this brain from bits of that one
|
||||
// (presumably this one is made from a single sample)
|
||||
void brain::resynth(const string &filename, const brain &other, float ratio){
|
||||
void brain::resynth(const string &filename, const brain &other, float ratio, u32 fftwack){
|
||||
sample out((m_block_size-m_overlap)*m_blocks.size());
|
||||
out.zero();
|
||||
u32 pos = 0;
|
||||
@ -95,7 +95,7 @@ void brain::resynth(const string &filename, const brain &other, float ratio){
|
||||
for (vector<block>::iterator i=m_blocks.begin(); i!=m_blocks.end(); ++i) {
|
||||
cerr<<'\r';
|
||||
cerr<<"searching: "<<count/float(m_blocks.size())*100;
|
||||
u32 index = other.search(*i,ratio);
|
||||
u32 index = other.search(*i,ratio, fftwack);
|
||||
//cerr<<index<<endl;
|
||||
out.mul_mix(other.get_block_pcm(index),pos,0.2);
|
||||
|
||||
@ -141,10 +141,10 @@ bool brain::unit_test() {
|
||||
|
||||
b2.init(512, 0, 20);
|
||||
b3.init(512, 0, 20);
|
||||
assert(b3.search(b2.m_blocks[0],1)==0);
|
||||
assert(b3.search(b2.m_blocks[9],1)==9);
|
||||
assert(b3.search(b2.m_blocks[19],1)==19);
|
||||
assert(b3.search(b2.m_blocks[29],1)==29);
|
||||
assert(b3.search(b2.m_blocks[0],1,0)==0);
|
||||
assert(b3.search(b2.m_blocks[9],1,0)==9);
|
||||
assert(b3.search(b2.m_blocks[19],1,0)==19);
|
||||
assert(b3.search(b2.m_blocks[29],1,0)==29);
|
||||
|
||||
// sample r = b2.resynth(b,1);
|
||||
// assert(r.get_length()==200);
|
||||
|
@ -21,7 +21,7 @@ public:
|
||||
sample load_sound(std::string filename);
|
||||
// take another brain and rebuild this brain from bits of that one
|
||||
// (presumably this one is made from a single sample)
|
||||
void resynth(const std::string &filename, const brain &other, float ratio);
|
||||
void resynth(const std::string &filename, const brain &other, float ratio, u32 fftwack);
|
||||
|
||||
const sample &get_block_pcm(u32 index) const;
|
||||
const block &get_block(u32 index) const;
|
||||
@ -29,7 +29,7 @@ public:
|
||||
const u32 get_block_size() const { return m_block_size; }
|
||||
const u32 get_overlap() const { return m_overlap; }
|
||||
|
||||
u32 search(const block &target, float ratio) const;
|
||||
u32 search(const block &target, float ratio, u32 fftwack) const;
|
||||
|
||||
static bool unit_test();
|
||||
|
||||
|
@ -64,6 +64,8 @@ int main(int argc, char *argv[])
|
||||
source.load_sound("../sound/source/pw3.wav");
|
||||
source.load_sound("../sound/source/claps.wav");
|
||||
source.load_sound("../sound/source/eagle.wav");
|
||||
// source.load_sound("../sound/source/sailingbybit.wav");
|
||||
|
||||
|
||||
//target.load_sound("../sound/source/sb-left.wav");
|
||||
target.load_sound("../sound/source/apache.wav");
|
||||
|
@ -24,6 +24,9 @@ void renderer::process(u32 nframes, float *buf) {
|
||||
cerr<<'\r';
|
||||
cerr<<m_ratio;
|
||||
|
||||
u32 fftwack = 0; //100*(sin((ratio_time++)*0.01)*0.5+0.5);
|
||||
//m_ratio = 0;
|
||||
|
||||
if (tgt_end>=m_target.get_num_blocks()) {
|
||||
m_render_time=0;
|
||||
m_render_blocks.clear();
|
||||
@ -38,7 +41,7 @@ void renderer::process(u32 nframes, float *buf) {
|
||||
// get indices for current buffer
|
||||
for (u32 tgt_index = tgt_start; tgt_index<=tgt_end; tgt_index++) {
|
||||
u32 time=tgt_index*tgt_shift;
|
||||
u32 src_index = m_source.search(m_target.get_block(tgt_index), m_ratio);
|
||||
u32 src_index = m_source.search(m_target.get_block(tgt_index), m_ratio, fftwack);
|
||||
// put them in the index list
|
||||
m_render_blocks.push_back(render_block(src_index,time));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user