windows loading fix 64bit and mic in cleanup

This commit is contained in:
dave griffiths 2016-09-24 10:33:16 +01:00
parent 3deaf9ac1e
commit cd6b842026
12 changed files with 306 additions and 166 deletions

View File

@ -48,7 +48,8 @@ audio_thread::~audio_thread() {
void audio_thread::start_audio() {
if (m_audio_device!=NULL) delete m_audio_device;
m_audio_device = new audio_device("samplebrain",44100,2048);
m_audio_device = new audio_device("samplebrain",48000,2048);
//m_audio_device = new audio_device("samplebrain",48000,2048*4);
m_audio_device->m_client.set_callback(run_audio, this);
}

View File

@ -14,6 +14,7 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <unistd.h>
#include <iostream>
#include "block_stream.h"
@ -22,6 +23,7 @@ using namespace std;
#define BUFFER_SIZE 4096*10
#define MAX_BLOCKS 200
#define NUM_WORKERS 4
block_stream::block_stream() :
m_ready(false),
@ -29,8 +31,13 @@ block_stream::block_stream() :
m_block_position(0),
m_buffer_position(0),
m_buffer(BUFFER_SIZE),
m_block_index_offset(0)
m_block_index_offset(0),
m_sent_block_index(0)
{
for (u32 i=0; i<NUM_WORKERS; ++i) {
m_workers.push_back(new worker(i,&m_window));
usleep(1);
}
}
block_stream::~block_stream() {}
@ -38,16 +45,21 @@ block_stream::~block_stream() {}
void block_stream::init(u32 block_size, u32 overlap, window::type t, bool ditchpcm) {
m_block_size=block_size;
m_overlap=overlap;
m_block_index=0;
m_block_index=20;
m_block_position=0;
m_buffer_position=0;
if (m_overlap>=m_block_size) m_overlap=0;
cerr<<m_block_size<<endl;
m_window.init(m_block_size);
m_window.set_current_type(t);
m_blocks.clear();
sample dummy(block_size);
for (u32 i=0; i<MAX_BLOCKS; i++) {
m_blocks.push_back(block(0,"dummy",dummy,44100,m_window));
}
m_ready=true;
}
@ -69,9 +81,17 @@ void block_stream::process(const sample &left, const sample &right) {
//cerr<<(s32)(m_buffer_position-m_block_size)<<" to "<<m_buffer_position<<endl;
m_buffer.get_region(region,(s32)(m_buffer_position-m_block_size),
m_buffer_position);
m_blocks.push_back(block(0,"input",region,44100,m_window));
//cerr<<"starting block "<<m_block_index<<endl;
scatter_gather(m_block_index,region);
m_block_index=(m_block_index+1)%MAX_BLOCKS;
//block b(0,"input",region,44100,m_window);
//m_blocks.push_back(b);
m_block_position=0;
if (m_blocks.size()>MAX_BLOCKS) {
m_blocks.erase(m_blocks.begin());
m_block_index_offset++;
@ -80,9 +100,70 @@ void block_stream::process(const sample &left, const sample &right) {
m_block_position++;
}
cerr<<"num blocks: "<<m_blocks.size()<<endl;
}
const block &block_stream::get_block(u32 index) const {
return m_blocks[(index-m_block_index_offset)%m_blocks.size()];
//cerr<<"returning "<<(index-m_block_index_offset)%m_blocks.size()<<" ("<<m_blocks.size()<<")"<<endl;
u32 i = (index-m_block_index_offset)%MAX_BLOCKS;
//if (m_block_index<i) {
// cerr<<i<<" ("<<(s32)(m_block_index-(s32)(i))<<")"<<endl;
//}
return m_blocks[i];
}
//-----------------------------------------------------------
void _run_worker(void *p) {
((block_stream::worker *)p)->run();
}
block_stream::worker::worker(u32 id, window *w) :
m_id(id),
m_status(READY),
m_window(w)
{
m_worker_mutex = new pthread_mutex_t;
pthread_mutex_init(m_worker_mutex,NULL);
m_thread = new pthread_t;
pthread_create(m_thread,NULL,(void*(*)(void*))_run_worker,this);
}
void block_stream::worker::run() {
//cerr<<"worker "<<m_id<<" started..."<<endl;
while (true) {
pthread_mutex_lock(m_worker_mutex);
if (m_status==ACTIVATE) {
cerr<<"starting "<<m_id<<endl;
m_status=WORKING;
m_output = new block(0,"input",m_region,44100,*m_window);
m_status=FINISHED;
cerr<<"ending "<<m_id<<endl;
}
pthread_mutex_unlock(m_worker_mutex);
usleep(5);
}
}
void block_stream::scatter_gather(u32 block_index, const sample &region) {
while(true) {
for (auto &w : m_workers) {
if (pthread_mutex_trylock(w->m_worker_mutex)) {
if (w->m_status == worker::FINISHED) {
//cerr<<"adding finished block "<<w->m_block_index<<endl;
m_blocks[w->m_block_index]=*w->m_output;
w->m_status = worker::READY;
}
if (w->m_status == worker::READY) {
w->m_region = region;
w->m_status = worker::ACTIVATE;
w->m_block_index = block_index;
return;
}
pthread_mutex_unlock(w->m_worker_mutex);
}
}
}
}

View File

@ -17,6 +17,7 @@
#include <vector>
#include "window.h"
#include "block.h"
#include "jellyfish/sample.h"
#include "block_source.h"
#ifndef BLOCK_STREAM
@ -39,10 +40,33 @@ class block_stream : public block_source {
virtual const block &get_block(u32 index) const;
virtual u32 get_num_blocks() const { return UINT_MAX; }
u32 last_block_index() const { return m_block_index_offset+m_blocks.size(); }
u32 last_block_index() const { return m_block_index_offset+m_blocks.size()-1; }
class worker {
public:
worker(u32 id, window *w);
enum worker_status { READY=0, ACTIVATE, WORKING, FINISHED };
void run();
u32 m_id;
worker_status m_status;
pthread_mutex_t* m_worker_mutex;
sample m_region;
block *m_output;
window *m_window;
u32 m_block_index;
pthread_t *m_thread;
};
private:
void scatter_gather(u32 block_index, const sample &region);
vector<worker*> m_workers;
bool m_ready;
u32 m_block_index;
@ -55,6 +79,10 @@ class block_stream : public block_source {
u32 m_block_index_offset;
vector<block> m_blocks;
mutable s32 m_sent_block_index;
block *m_dummy_block;
};
}

View File

@ -55,8 +55,14 @@ bool portaudio_client::attach(const string &client_name, const device_options &d
fprintf( stderr, "error message: %s\n", Pa_GetErrorText( err ) );
}
PaDeviceIndex output_device_num = Pa_GetDefaultOutputDevice();
PaDeviceIndex input_device_num = Pa_GetDefaultInputDevice();
//output_device_num = 4;
//input_device_num = 4;
PaStreamParameters output_parameters;
output_parameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
output_parameters.device = output_device_num;
if (output_parameters.device == paNoDevice) {
cerr<<"error: no default output device."<<endl;
}
@ -65,8 +71,10 @@ bool portaudio_client::attach(const string &client_name, const device_options &d
output_parameters.suggestedLatency = Pa_GetDeviceInfo( output_parameters.device )->defaultLowOutputLatency;
output_parameters.hostApiSpecificStreamInfo = NULL;
cerr<<"Connecting to "<<Pa_GetDeviceInfo( output_parameters.device )->name<<" for output"<<endl;
PaStreamParameters input_parameters;
input_parameters.device = Pa_GetDefaultInputDevice(); /* default output device */
input_parameters.device = input_device_num;
if (input_parameters.device == paNoDevice) {
cerr<<"error: no default input device."<<endl;
}
@ -75,6 +83,8 @@ bool portaudio_client::attach(const string &client_name, const device_options &d
input_parameters.suggestedLatency = Pa_GetDeviceInfo( input_parameters.device )->defaultLowInputLatency;
input_parameters.hostApiSpecificStreamInfo = NULL;
cerr<<"Connecting to "<<Pa_GetDeviceInfo( input_parameters.device )->name<<" for input"<<endl;
PaStream *stream;
err = Pa_OpenStream(&stream,

View File

@ -26,8 +26,8 @@ template<>ios &spiralcore::operator||(ios &s, string &v) {
ofstream *pos=dynamic_cast<ofstream*>(&s);
if (pos!=NULL) {
ofstream &os = *pos;
size_t len = v.length();
os.write((char *)&len,sizeof(size_t));
u64 len = v.length();
os.write((char *)&len,sizeof(u64));
os.write((char*)(v.c_str()),v.length());
return os;
}
@ -36,8 +36,8 @@ template<>ios &spiralcore::operator||(ios &s, string &v) {
ifstream *pis=dynamic_cast<ifstream*>(&s);
assert(pis!=NULL);
ifstream &is = *pis;
size_t len=0;
is.read((char *)&len,sizeof(size_t));
u64 len=0;
is.read((char *)&len,sizeof(u64));
if (len>0) {
char *str = new char[len+1];
is.read(str,len);

View File

@ -25,6 +25,7 @@
#include "block.h"
#include "brain.h"
#include "renderer.h"
#include "block_stream.h"
#include <pthread.h>
using namespace std;
@ -46,12 +47,23 @@ void unit_test() {
}
audio_device *a = NULL;
block_stream *m_block_stream = NULL;
bool m_mic_mode = true;
void run_audio(void* c, unsigned int frames) {
a->left_out.zero();
renderer *rr = (renderer*)c;
pthread_mutex_lock(m_fuz_mutex);
rr->process(frames,a->left_out.get_non_const_buffer());
block_stream *bs=NULL;
if (m_mic_mode) {
m_block_stream->process(a->left_in,a->right_in);
bs = m_block_stream;
}
rr->process(frames,a->left_out.get_non_const_buffer(),bs);
pthread_mutex_unlock(m_fuz_mutex);
a->right_out=a->left_out;
a->maybe_record();
@ -127,8 +139,15 @@ void fuz() {
brain target;
target.load_sound(fuz_samplefile(),brain::MIX);
u32 len=fuz_rr_i(500,5000);
target.init(len,len-len/fuz_rr_i(1,16),window::HANN);
// u32 len=fuz_rr_i(500,5000);
//u32 overlap = len-len/fuz_rr_i(1,16);
u32 len=5000;
u32 overlap=len/2;
target.init(len,overlap,window::HANN);
m_block_stream = new block_stream();
m_block_stream->init(len,overlap,window::HANN);
cerr<<"ready..."<<endl;
cerr<<"we have "<<source.get_num_blocks()<<" brain blocks ("<<source.get_num_blocks()*len/44100.0<<" secs)"<<endl<<endl;
@ -199,6 +218,8 @@ int main(int argc, char *argv[])
{
m_fuz_mutex = new pthread_mutex_t;
pthread_mutex_init(m_fuz_mutex,NULL);
unit_test();
//unit_test();
fuz();
}

View File

@ -57,15 +57,14 @@ void renderer::process(u32 nframes, float *buf, const block_stream *bs) {
// or the realtime block stream
const block_source *source = (block_source*)bs;
if (source==NULL) {
cerr<<"not using block stream..."<<endl;
source = &m_target;
} else {
if (m_target_index<bs->last_block_index()-10) {
cerr<<"catch up..."<<endl;
if ((s32)m_target_index<(s32)(bs->last_block_index()-100)) {
//cerr<<"catch up... "<<(s32)m_target_index<<" "<<(s32)bs->last_block_index()<<endl;
m_target_index = bs->last_block_index();
}
if (m_target_index>bs->last_block_index()) {
cerr<<"catch down..."<<endl;
//cerr<<"catch down..."<<endl;
m_target_index = bs->last_block_index();
}
}
@ -106,7 +105,7 @@ bool renderer::find_render_blocks(const block_source &target, u32 nframes) {
}
cerr<<"-----------------"<<endl;
/* cerr<<"-----------------"<<endl;
cerr<<"tgt start:"<<m_target_index<<endl;
cerr<<"tgt end:"<<tgt_end<<endl;
cerr<<":"<<tgt_end-m_target_index<<endl;
@ -117,7 +116,7 @@ bool renderer::find_render_blocks(const block_source &target, u32 nframes) {
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;
*/
// search phase
// get indices for current buffer