samplebrain/samplebrain/qt/process_thread.cpp

95 lines
3.0 KiB
C++
Raw Normal View History

2015-07-11 15:03:05 -03:00
#include "process_thread.h"
#include <iostream>
2015-07-14 08:17:55 -03:00
#include <unistd.h>
2015-07-11 15:03:05 -03:00
using namespace spiralcore;
using namespace std;
2015-07-11 17:30:16 -03:00
static void _process(void *c) {
process_thread *p=(process_thread*)c;
p->process();
}
2015-07-11 15:03:05 -03:00
process_thread::process_thread() :
2015-07-11 17:30:16 -03:00
m_osc("8889"),
m_source_block_size(3000),
m_source_overlap(0.75),
m_target_block_size(3000),
m_target_overlap(0.75),
2015-07-18 20:34:56 -03:00
m_window_type(window::DODGY),
m_target_window_type(window::DODGY)
2015-07-11 15:03:05 -03:00
{
m_brain_mutex = new pthread_mutex_t;
pthread_mutex_init(m_brain_mutex,NULL);
m_osc.run();
// start the processing thread
m_thread = new pthread_t;
pthread_create(m_thread,NULL,(void*(*)(void*))_process,this);
}
2015-07-11 15:03:05 -03:00
process_thread::~process_thread() {
pthread_cancel(*m_thread);
delete m_brain_mutex;
2015-07-11 15:03:05 -03:00
}
2015-07-11 17:30:16 -03:00
void process_thread::process() {
2015-07-11 15:03:05 -03:00
command_ring_buffer::command cmd;
while(true) {
2015-07-11 17:30:16 -03:00
while (m_osc.get(cmd)) {
2015-07-11 15:03:05 -03:00
string name = cmd.m_name;
cerr<<name<<endl;
2015-07-11 17:30:16 -03:00
if (name=="/load_sample") {
pthread_mutex_lock(m_brain_mutex);
m_source.load_sound(cmd.get_string(0));
pthread_mutex_unlock(m_brain_mutex);
}
if (name=="/delete_sample") {
pthread_mutex_lock(m_brain_mutex);
m_source.delete_sound(cmd.get_string(0));
pthread_mutex_unlock(m_brain_mutex);
}
if (name=="/source_block_size") {
m_source_block_size = cmd.get_int(0);
}
if (name=="/source_overlap") {
2015-07-12 20:26:30 -03:00
m_source_overlap = m_source_block_size*cmd.get_float(0);
2015-07-11 17:30:16 -03:00
}
if (name=="/generate_brain") {
pthread_mutex_lock(m_brain_mutex);
2015-07-18 20:34:56 -03:00
cerr<<m_window_type<<endl;
m_source.init(m_source_block_size, m_source_overlap, m_window_type);
2015-07-11 17:30:16 -03:00
pthread_mutex_unlock(m_brain_mutex);
}
if (name=="/load_target") {
pthread_mutex_lock(m_brain_mutex);
m_target.clear_sounds();
m_target.load_sound(cmd.get_string(0));
pthread_mutex_unlock(m_brain_mutex);
}
if (name=="/target_block_size") {
m_target_block_size = cmd.get_int(0);
}
if (name=="/target_overlap") {
2015-07-12 20:26:30 -03:00
m_target_overlap = m_target_block_size*cmd.get_float(0);
cerr<<m_target_overlap<<endl;
2015-07-11 17:30:16 -03:00
}
if (name=="/generate_target") {
pthread_mutex_lock(m_brain_mutex);
2015-07-18 20:34:56 -03:00
cerr<<m_target_window_type<<endl;
m_target.init(m_target_block_size, m_target_overlap, m_target_window_type);
2015-07-11 17:30:16 -03:00
pthread_mutex_unlock(m_brain_mutex);
2015-07-11 15:03:05 -03:00
}
2015-07-18 20:34:56 -03:00
if (name=="/window_type") {
m_window_type=(window::type)cmd.get_int(0);
}
if (name=="/target_window_type") {
m_target_window_type=(window::type)cmd.get_int(0);
}
2015-07-11 15:03:05 -03:00
}
usleep(500);
}
}