samplebrain/brain/src/renderer.cpp

317 lines
9.3 KiB
C++
Raw Normal View History

2022-09-08 08:21:53 -03:00
// Copyright (C) 2022 Then Try This
2015-07-21 10:58:13 -03:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2015-07-09 17:50:03 -03:00
#include "renderer.h"
#include <iostream>
using namespace spiralcore;
using namespace std;
2015-07-11 08:29:51 -03:00
void renderer::init(brain &source, brain &target) {
2015-09-23 14:35:13 -03:00
m_volume=1;
m_playing=false;
m_source=source;
m_target=target;
m_target_time=0;
m_render_time=0;
m_n_mix=0;
m_target_mix=0;
m_render_blocks.clear();
m_search_algo=BASIC;
m_slide_error=1;
m_target_index=0;
m_render_index=0;
m_stretch=1;
m_last_tgt_shift=0;
m_autotune=0;
2015-07-09 17:50:03 -03:00
}
2015-07-09 19:54:05 -03:00
static int ratio_time = 0;
2015-07-27 12:17:49 -03:00
void renderer::reset() {
2015-09-23 14:35:13 -03:00
m_target_time=0;
m_render_time=0;
m_target_index=0;
m_render_index=0;
m_render_blocks.clear();
m_source.jiggle();
2015-07-27 12:17:49 -03:00
}
2015-08-05 18:09:08 -03:00
2016-07-08 07:41:30 -03:00
void renderer::process(u32 nframes, float *buf, const block_stream *bs) {
// we can get blocks from the preprocessed target brain
// or the realtime block stream
const block_source *source = (block_source*)bs;
if (source==NULL) {
source = &m_target;
} else {
if ((s32)m_target_index<(s32)(bs->last_block_index()-100)) {
//cerr<<"catch up... "<<(s32)m_target_index<<" "<<(s32)bs->last_block_index()<<endl;
2016-07-08 07:41:30 -03:00
m_target_index = bs->last_block_index();
}
if (m_target_index>bs->last_block_index()) {
//cerr<<"catch down..."<<endl;
2016-07-08 07:41:30 -03:00
m_target_index = bs->last_block_index();
}
}
2015-09-23 14:35:13 -03:00
if (!m_playing) return;
2016-07-08 07:41:30 -03:00
if (!find_render_blocks(*source,nframes)) return;
2015-08-05 18:09:08 -03:00
2016-07-08 07:41:30 -03:00
render(*source,nframes,buf);
2015-08-05 18:09:08 -03:00
2015-09-23 14:35:13 -03:00
clean_up();
2015-08-05 18:09:08 -03:00
2015-09-23 14:35:13 -03:00
m_render_time+=nframes;
//m_target_time+=nframes/(float)m_stretch;
2015-08-05 18:09:08 -03:00
}
2015-08-06 18:21:30 -03:00
// target_time = samples time into target stream
// render_time = position in output stream, updated per process - used for offsets
2016-07-08 07:41:30 -03:00
bool renderer::find_render_blocks(const block_source &target, u32 nframes) {
2015-09-23 14:35:13 -03:00
// get new blocks from source for the current buffer
2016-07-08 07:41:30 -03:00
// figure out where are in the target blocks
u32 tgt_shift = target.get_block_size()-target.get_overlap();
2015-09-23 14:35:13 -03:00
m_target_index = m_target_time/(float)tgt_shift;
u32 tgt_end = (m_target_time+nframes)/(float)tgt_shift;
2016-07-08 07:41:30 -03:00
// render end
2015-09-23 14:35:13 -03:00
u32 rnd_end = (m_render_time+nframes)/(float)tgt_shift;
2016-07-08 07:41:30 -03:00
// when stuff has changed, or we have fallen off the end
// then recompute and abort
2015-09-23 14:35:13 -03:00
if (tgt_shift!=m_last_tgt_shift ||
2016-07-08 07:41:30 -03:00
tgt_end>=target.get_num_blocks() ||
2015-09-23 14:35:13 -03:00
m_source.get_num_blocks()==0) {
reset();
m_last_tgt_shift = tgt_shift;
// next time...
return false;
}
2016-07-08 07:41:30 -03:00
/* cerr<<"-----------------"<<endl;
2015-08-06 09:39:14 -03:00
cerr<<"tgt start:"<<m_target_index<<endl;
cerr<<"tgt end:"<<tgt_end<<endl;
cerr<<":"<<tgt_end-m_target_index<<endl;
cerr<<"target time "<<m_target_time<<endl;
2015-08-06 18:21:30 -03:00
cerr<<"target time (index) "<<m_target_index*tgt_shift<<endl;
cerr<<"real vs index = "<<(s32)m_target_time-(s32)(m_target_index*tgt_shift)<<endl;
2015-08-06 09:39:14 -03:00
cerr<<"render time "<<m_render_time<<endl;
2015-08-06 18:21:30 -03:00
cerr<<"render time (index) "<<m_render_index*tgt_shift<<endl;
cerr<<"real vs index = "<<(s32)m_render_time-(s32)(m_render_index*tgt_shift)<<endl;
cerr<<m_render_blocks.size()<<endl;
*/
2015-09-23 14:35:13 -03:00
// search phase
// get indices for current buffer
u32 counter = m_render_index;
//u32 cur_time = m_render_time;
while (counter<=rnd_end) {
u32 time=m_render_index*tgt_shift;
u32 src_index=0;
2016-07-08 07:41:30 -03:00
// which algo are we using today?
2015-09-23 14:35:13 -03:00
switch (m_search_algo) {
case BASIC:
2016-07-08 07:41:30 -03:00
src_index = m_source.search(target.get_block(m_target_index), m_search_params);
2015-09-23 14:35:13 -03:00
break;
case REV_BASIC:
2016-07-08 07:41:30 -03:00
src_index = m_source.rev_search(target.get_block(m_target_index), m_search_params);
2015-09-23 14:35:13 -03:00
break;
case SYNAPTIC:
case SYNAPTIC_SLIDE:
2016-07-08 07:41:30 -03:00
src_index = m_source.search_synapses(target.get_block(m_target_index), m_search_params);
2015-09-23 14:35:13 -03:00
break;
2015-08-05 18:09:08 -03:00
}
2015-09-23 14:35:13 -03:00
if (m_search_algo==SYNAPTIC_SLIDE) {
m_render_blocks.push_back(render_block(src_index,m_target_index,time));
2016-07-08 07:41:30 -03:00
// synaptic slide blocks progression of the target until
// a good enough source block is found
2015-09-23 14:35:13 -03:00
if (m_source.get_current_error()<m_slide_error &&
m_render_index%m_stretch==0) {
m_target_index++;
m_target_time+=tgt_shift;
}
} else {
// put them in the index list
m_render_blocks.push_back(render_block(src_index,m_target_index,time));
if (m_render_index%m_stretch==0) {
m_target_index++;
m_target_time+=tgt_shift;
}
2015-08-05 18:09:08 -03:00
}
2015-09-23 14:35:13 -03:00
counter++;
m_render_index++;
}
return true;
2015-08-05 18:09:08 -03:00
}
2016-07-08 07:41:30 -03:00
void renderer::render(const block_source &target, u32 nframes, float *buf) {
2015-09-23 14:35:13 -03:00
sample render_pcm(m_source.get_block_size());
// render phase
// render all blocks in list
for (std::list<render_block>::iterator i=m_render_blocks.begin(); i!=m_render_blocks.end(); ++i) {
2016-07-08 07:41:30 -03:00
// get all the pcm data now
2015-09-23 14:35:13 -03:00
const sample &pcm=m_source.get_block(i->m_index).get_pcm();
const sample &n_pcm=m_source.get_block(i->m_index).get_n_pcm();
2016-07-08 07:41:30 -03:00
const sample &target_pcm=target.get_block(i->m_tgt_index).get_pcm();
2015-09-23 14:35:13 -03:00
// get the sample offset into the buffer
s32 offset = i->m_time-m_render_time;
2015-09-25 05:14:01 -03:00
u32 block_length = pcm.get_length();
2015-09-23 14:35:13 -03:00
// assume midway through block
u32 block_start = offset;
u32 buffer_start = 0;
if (offset<0) {
2016-07-08 07:41:30 -03:00
// it was running before this buffer
2015-09-23 14:35:13 -03:00
block_start=-offset;
2015-09-25 05:14:01 -03:00
if (block_start>=block_length &&
i->m_position>=block_length) {
i->m_finished=true;
}
2015-09-23 14:35:13 -03:00
} else { // block is midway through buffer
block_start=0;
buffer_start=offset;
2015-08-05 18:09:08 -03:00
}
2015-09-23 14:35:13 -03:00
// cerr<<"-----------------"<<endl;
// cerr<<"block start:"<<block_start<<endl;
// cerr<<"buffer start:"<<buffer_start<<endl;
2015-08-05 18:09:08 -03:00
2015-09-23 14:35:13 -03:00
// fade in/out autotune
2015-09-25 05:14:01 -03:00
//pitch_scale = pitch_scale*m_autotune + 1.0f*(1-m_autotune);
2015-08-05 18:09:08 -03:00
2015-09-25 05:14:01 -03:00
float pitch_scale = 1;
2015-08-05 18:09:08 -03:00
2015-09-25 05:14:01 -03:00
if (m_autotune>0) {
2016-07-08 07:41:30 -03:00
// scale by ratio between target and source
pitch_scale = target.get_block(i->m_tgt_index).get_freq() /
2015-09-25 05:14:01 -03:00
m_source.get_block(i->m_index).get_freq();
2016-07-08 07:41:30 -03:00
// restrict min/max pitch bend
2015-09-25 05:14:01 -03:00
float max = 1+(m_autotune*m_autotune)*100.0f;
if (pitch_scale>(max)) pitch_scale=max;
if (pitch_scale<(1/max)) pitch_scale=1/max;
}
2016-07-08 07:41:30 -03:00
// pitchshifting sounded rubbish with such small clips
2015-09-25 05:14:01 -03:00
//pitchshift::process(pcm,pitch_scale,render_pcm);
2015-08-05 18:09:08 -03:00
2015-09-23 14:35:13 -03:00
if (!i->m_finished) {
// mix in
u32 buffer_pos = buffer_start;
u32 block_pos = block_start;
u32 block_end = pcm.get_length();
2015-08-05 18:09:08 -03:00
2015-09-25 05:14:01 -03:00
while (i->m_position<block_end && buffer_pos<nframes) {
2015-08-05 18:09:08 -03:00
2015-09-23 14:35:13 -03:00
// mix with normalised version
float brain_sample = (pcm[i->m_position]*(1-m_n_mix)+
2015-09-25 05:14:01 -03:00
n_pcm[i->m_position]*m_n_mix);
float target_sample = 0;
2015-07-11 08:29:51 -03:00
2015-09-25 05:14:01 -03:00
// if playback scale is lower than target then we may
// run off the end of the target block
if (block_pos<block_length) {
// for mixing with target audio
target_sample = target_pcm[block_pos];
}
2015-07-09 18:59:44 -03:00
2015-09-23 14:35:13 -03:00
buf[buffer_pos]+=(brain_sample*(1-m_target_mix) +
target_sample*m_target_mix)*0.2*m_volume;
2015-07-09 17:50:03 -03:00
2015-09-23 14:35:13 -03:00
i->m_position+=pitch_scale;
2015-07-09 17:50:03 -03:00
2015-09-25 05:14:01 -03:00
// repeat fast blocks if we are still playing the source
if (block_pos<block_length &&
i->m_position>block_end) {
i->m_position=0;
}
2015-09-23 14:35:13 -03:00
++buffer_pos;
++block_pos;
}
2015-07-09 17:50:03 -03:00
}
2015-09-23 14:35:13 -03:00
}
2015-07-09 17:50:03 -03:00
}
2015-09-23 14:35:13 -03:00
void renderer::clean_up() {
// cleanup phase
2016-07-08 07:41:30 -03:00
// delete old render blocks that have finished
2015-09-23 14:35:13 -03:00
std::list<render_block>::iterator i=m_render_blocks.begin();
std::list<render_block>::iterator ni=m_render_blocks.begin();
while(i!=m_render_blocks.end()) {
ni++;
if (i->m_finished) m_render_blocks.erase(i);
i=ni;
}
}
2015-08-05 18:09:08 -03:00
2015-10-16 12:23:51 -03:00
ios &spiralcore::operator||(ios &s, renderer &r) {
u32 version=0;
string id("renderer");
s||id||version;
s||r.m_search_params;
s||r.m_volume||r.m_playing||r.m_target_index||r.m_render_index;
s||r.m_target_time||r.m_render_time||r.m_stretch;
s||r.m_n_mix||r.m_target_mix||r.m_autotune;
s||r.m_search_algo||r.m_slide_error||r.m_last_tgt_shift;
return s;
}
2015-07-09 17:50:03 -03:00
bool renderer::unit_test() {
2015-09-23 14:35:13 -03:00
brain source;
2016-02-03 13:45:09 -02:00
source.load_sound("test_data/up.wav", brain::MIX);
2015-09-23 14:35:13 -03:00
source.init(10,0,window::RECTANGLE);
brain target;
2016-02-03 13:45:09 -02:00
target.load_sound("test_data/up.wav", brain::MIX);
2015-09-23 14:35:13 -03:00
target.init(10,0,window::RECTANGLE);
renderer rr(source,target);
rr.set_playing(true);
float *buf=new float[400];
rr.process(10,buf);
rr.process(10,buf);
assert(rr.m_render_blocks.size()==2);
rr.process(10,buf);
assert(rr.m_render_blocks.size()==2);
delete[] buf;
buf=new float[20];
rr.process(20,buf);
assert(rr.m_render_blocks.size()==3);
rr.process(5,buf);
assert(rr.m_render_blocks.size()==1);
target.init(10,5,window::RECTANGLE);
rr.process(10,buf);
rr.process(10,buf);
rr.process(10,buf);
rr.process(10,buf);
assert(rr.m_render_blocks.size()==4);
delete[] buf;
return true;
2015-07-09 17:50:03 -03:00
}