mirror of
https://gitlab.com/then-try-this/samplebrain.git
synced 2025-05-12 10:37:20 +00:00
ok so far...
This commit is contained in:
parent
ef769a036d
commit
6876ae9031
@ -25,6 +25,8 @@ sample brain::load_sound(std::string filename) {
|
|||||||
// rewrites whole brain
|
// rewrites whole brain
|
||||||
void brain::init(u32 block_size, u32 overlap) {
|
void brain::init(u32 block_size, u32 overlap) {
|
||||||
m_blocks.clear();
|
m_blocks.clear();
|
||||||
|
m_block_size = block_size;
|
||||||
|
m_overlap = overlap;
|
||||||
for (vector<sample>::iterator i=m_samples.begin(); i!=m_samples.end(); ++i) {
|
for (vector<sample>::iterator i=m_samples.begin(); i!=m_samples.end(); ++i) {
|
||||||
chop_and_add(*i, block_size, overlap);
|
chop_and_add(*i, block_size, overlap);
|
||||||
}
|
}
|
||||||
@ -40,12 +42,17 @@ void brain::chop_and_add(const sample &s, u32 block_size, u32 overlap) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sample &brain::get_block_pcm(u32 index) const {
|
||||||
|
return m_blocks[index].get_pcm();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// returns index to block
|
// returns index to block
|
||||||
u32 brain::search(const brain_block &target, float ratio) {
|
u32 brain::search(const brain_block &target, float ratio) const {
|
||||||
double closest = 999999999;
|
double closest = 999999999;
|
||||||
u32 closest_index = 0;
|
u32 closest_index = 0;
|
||||||
u32 index = 0;
|
u32 index = 0;
|
||||||
for (vector<brain_block>::iterator i=m_blocks.begin(); i!=m_blocks.end(); ++i) {
|
for (vector<brain_block>::const_iterator i=m_blocks.begin(); i!=m_blocks.end(); ++i) {
|
||||||
double diff = target.compare(*i,ratio);
|
double diff = target.compare(*i,ratio);
|
||||||
if (diff<closest) {
|
if (diff<closest) {
|
||||||
closest=diff;
|
closest=diff;
|
||||||
@ -59,8 +66,14 @@ u32 brain::search(const brain_block &target, float ratio) {
|
|||||||
// take another brain and rebuild this brain from bits of that one
|
// take another brain and rebuild this brain from bits of that one
|
||||||
// (presumably this one is made from a single sample)
|
// (presumably this one is made from a single sample)
|
||||||
sample brain::resynth(const brain &other, float ratio){
|
sample brain::resynth(const brain &other, float ratio){
|
||||||
|
sample out(m_block_size*m_blocks.size());
|
||||||
|
u32 pos=0;
|
||||||
|
for (vector<brain_block>::iterator i=m_blocks.begin(); i!=m_blocks.end(); ++i) {
|
||||||
|
u32 index = other.search(*i,ratio);
|
||||||
|
out.mix(other.get_block_pcm(index),pos);
|
||||||
|
pos += m_block_size-m_overlap;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -91,13 +104,15 @@ bool brain::unit_test() {
|
|||||||
brain b2;
|
brain b2;
|
||||||
b2.load_sound("test_data/100f32.wav");
|
b2.load_sound("test_data/100f32.wav");
|
||||||
b2.load_sound("test_data/100f32.wav");
|
b2.load_sound("test_data/100f32.wav");
|
||||||
assert(b.m_samples.size()==2);
|
assert(b2.m_samples.size()==2);
|
||||||
|
b2.init(10, 0);
|
||||||
b.init(10, 0);
|
b.init(10, 0);
|
||||||
assert(b.m_blocks.size()==20);
|
assert(b2.m_blocks.size()==20);
|
||||||
|
|
||||||
assert(b.search(b2.m_blocks[0],1)==0);
|
assert(b.search(b2.m_blocks[0],1)==0);
|
||||||
assert(b.search(b2.m_blocks[19],1)==19);
|
assert(b.search(b2.m_blocks[9],1)==9);
|
||||||
|
|
||||||
|
sample r = b2.resynth(b,1);
|
||||||
|
assert(r.get_length()==200);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ public:
|
|||||||
// todo: add tags
|
// todo: add tags
|
||||||
sample load_sound(std::string filename);
|
sample load_sound(std::string filename);
|
||||||
|
|
||||||
|
const sample &get_block_pcm(u32 index) const;
|
||||||
|
|
||||||
// take another brain and rebuild this brain from bits of that one
|
// take another brain and rebuild this brain from bits of that one
|
||||||
// (presumably this one is made from a single sample)
|
// (presumably this one is made from a single sample)
|
||||||
sample resynth(const brain &other, float ratio);
|
sample resynth(const brain &other, float ratio);
|
||||||
@ -28,12 +30,14 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
u32 search(const brain_block &target, float ratio);
|
u32 search(const brain_block &target, float ratio) const;
|
||||||
void chop_and_add(const sample &s, u32 block_size, u32 overlap);
|
void chop_and_add(const sample &s, u32 block_size, u32 overlap);
|
||||||
|
|
||||||
vector<brain_block> m_blocks;
|
vector<brain_block> m_blocks;
|
||||||
vector<sample> m_samples;
|
vector<sample> m_samples;
|
||||||
|
|
||||||
|
u32 m_block_size;
|
||||||
|
u32 m_overlap;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -85,8 +85,14 @@ bool brain_block::unit_test() {
|
|||||||
data[i]=i;
|
data[i]=i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
brain_block cpy("test",data,100);
|
||||||
|
{
|
||||||
brain_block bb3("test",data2,44100);
|
brain_block bb3("test",data2,44100);
|
||||||
assert(bb.compare(bb3,1)!=0);
|
assert(bb.compare(bb3,1)!=0);
|
||||||
|
cpy=bb3;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(cpy.m_pcm.get_length()==20);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ public:
|
|||||||
static void init_fft(u32 block_size);
|
static void init_fft(u32 block_size);
|
||||||
static bool unit_test();
|
static bool unit_test();
|
||||||
|
|
||||||
|
const sample &get_pcm() const { return m_pcm; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sample m_pcm;
|
sample m_pcm;
|
||||||
sample m_fft;
|
sample m_fft;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user