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() { void audio_thread::start_audio() {
if (m_audio_device!=NULL) delete m_audio_device; 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); 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 // along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <unistd.h>
#include <iostream> #include <iostream>
#include "block_stream.h" #include "block_stream.h"
@ -22,6 +23,7 @@ using namespace std;
#define BUFFER_SIZE 4096*10 #define BUFFER_SIZE 4096*10
#define MAX_BLOCKS 200 #define MAX_BLOCKS 200
#define NUM_WORKERS 4
block_stream::block_stream() : block_stream::block_stream() :
m_ready(false), m_ready(false),
@ -29,8 +31,13 @@ block_stream::block_stream() :
m_block_position(0), m_block_position(0),
m_buffer_position(0), m_buffer_position(0),
m_buffer(BUFFER_SIZE), 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() {} 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) { void block_stream::init(u32 block_size, u32 overlap, window::type t, bool ditchpcm) {
m_block_size=block_size; m_block_size=block_size;
m_overlap=overlap; m_overlap=overlap;
m_block_index=0; m_block_index=20;
m_block_position=0; m_block_position=0;
m_buffer_position=0; m_buffer_position=0;
if (m_overlap>=m_block_size) m_overlap=0; if (m_overlap>=m_block_size) m_overlap=0;
cerr<<m_block_size<<endl;
m_window.init(m_block_size); m_window.init(m_block_size);
m_window.set_current_type(t); m_window.set_current_type(t);
m_blocks.clear(); 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; 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; //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.get_region(region,(s32)(m_buffer_position-m_block_size),
m_buffer_position); 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; m_block_position=0;
if (m_blocks.size()>MAX_BLOCKS) { if (m_blocks.size()>MAX_BLOCKS) {
m_blocks.erase(m_blocks.begin()); m_blocks.erase(m_blocks.begin());
m_block_index_offset++; m_block_index_offset++;
@ -80,9 +100,70 @@ void block_stream::process(const sample &left, const sample &right) {
m_block_position++; m_block_position++;
} }
cerr<<"num blocks: "<<m_blocks.size()<<endl;
} }
const block &block_stream::get_block(u32 index) const { 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 <vector>
#include "window.h" #include "window.h"
#include "block.h" #include "block.h"
#include "jellyfish/sample.h"
#include "block_source.h" #include "block_source.h"
#ifndef BLOCK_STREAM #ifndef BLOCK_STREAM
@ -39,10 +40,33 @@ class block_stream : public block_source {
virtual const block &get_block(u32 index) const; virtual const block &get_block(u32 index) const;
virtual u32 get_num_blocks() const { return UINT_MAX; } 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: private:
void scatter_gather(u32 block_index, const sample &region);
vector<worker*> m_workers;
bool m_ready; bool m_ready;
u32 m_block_index; u32 m_block_index;
@ -55,6 +79,10 @@ class block_stream : public block_source {
u32 m_block_index_offset; u32 m_block_index_offset;
vector<block> m_blocks; vector<block> m_blocks;
mutable s32 m_sent_block_index;
block *m_dummy_block;
}; };
} }

View File

@ -72,7 +72,7 @@ char *command_ring_buffer::command::get_string(unsigned int index)
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
command_ring_buffer::command_ring_buffer(unsigned int size): command_ring_buffer::command_ring_buffer(unsigned int size):
ring_buffer(size) ring_buffer(size)
{ {
} }

View File

@ -23,7 +23,7 @@ static const unsigned int COMMAND_DATA_SIZE = 128;
class command_ring_buffer : public ring_buffer class command_ring_buffer : public ring_buffer
{ {
public: public:
command_ring_buffer(unsigned int size); command_ring_buffer(unsigned int size);
~command_ring_buffer(); ~command_ring_buffer();
@ -54,7 +54,7 @@ public:
bool send(const command& command); bool send(const command& command);
bool get(command& command); bool get(command& command);
private: private:
command m_current; command m_current;
}; };

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 ) ); 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; PaStreamParameters output_parameters;
output_parameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ output_parameters.device = output_device_num;
if (output_parameters.device == paNoDevice) { if (output_parameters.device == paNoDevice) {
cerr<<"error: no default output device."<<endl; 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.suggestedLatency = Pa_GetDeviceInfo( output_parameters.device )->defaultLowOutputLatency;
output_parameters.hostApiSpecificStreamInfo = NULL; output_parameters.hostApiSpecificStreamInfo = NULL;
cerr<<"Connecting to "<<Pa_GetDeviceInfo( output_parameters.device )->name<<" for output"<<endl;
PaStreamParameters input_parameters; PaStreamParameters input_parameters;
input_parameters.device = Pa_GetDefaultInputDevice(); /* default output device */ input_parameters.device = input_device_num;
if (input_parameters.device == paNoDevice) { if (input_parameters.device == paNoDevice) {
cerr<<"error: no default input device."<<endl; 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.suggestedLatency = Pa_GetDeviceInfo( input_parameters.device )->defaultLowInputLatency;
input_parameters.hostApiSpecificStreamInfo = NULL; input_parameters.hostApiSpecificStreamInfo = NULL;
cerr<<"Connecting to "<<Pa_GetDeviceInfo( input_parameters.device )->name<<" for input"<<endl;
PaStream *stream; PaStream *stream;
err = Pa_OpenStream(&stream, err = Pa_OpenStream(&stream,

View File

@ -23,11 +23,11 @@
using namespace std; using namespace std;
ring_buffer::ring_buffer(unsigned int size): ring_buffer::ring_buffer(unsigned int size):
m_read_pos(0), m_read_pos(0),
m_write_pos(0), m_write_pos(0),
m_size(size), m_size(size),
m_size_mask(size-1), m_size_mask(size-1),
m_buffer(NULL) m_buffer(NULL)
{ {
m_buffer = new char[m_size]; m_buffer = new char[m_size];
memset(m_buffer,'Z',m_size); memset(m_buffer,'Z',m_size);

View File

@ -22,7 +22,7 @@
class ring_buffer class ring_buffer
{ {
public: public:
ring_buffer(unsigned int size); ring_buffer(unsigned int size);
~ring_buffer(); ~ring_buffer();
@ -32,7 +32,7 @@ public:
bool read(char *dest, unsigned int size); bool read(char *dest, unsigned int size);
void dump(); void dump();
private: private:
unsigned int write_space(); unsigned int write_space();
unsigned int read_space(); unsigned int read_space();

View File

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

View File

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

View File

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