samplebrain/app/process_thread.cpp

209 lines
6.3 KiB
C++
Raw Normal View History

2015-07-21 10:58:13 -03:00
// Copyright (C) 2015 Dave Griffiths
//
// 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-11 15:03:05 -03:00
#include "process_thread.h"
#include <iostream>
2015-07-14 08:17:55 -03:00
#include <unistd.h>
#include "status.h"
2015-07-11 15:03:05 -03:00
2016-09-24 06:41:10 -03:00
#ifdef WIN32
#include <windows.h>
#endif
2015-07-11 15:03:05 -03:00
using namespace spiralcore;
using namespace std;
2022-09-10 10:05:45 -03:00
static void* _process(void *c) {
2016-07-08 07:41:30 -03:00
process_thread *p=(process_thread*)c;
p->process();
2022-09-10 10:05:45 -03:00
return NULL;
2015-07-11 17:30:16 -03:00
}
process_thread::process_thread(const string &port) :
m_osc(port),
m_source_block_size(1000),
2016-07-08 07:41:30 -03:00
m_source_overlap(0.75),
m_target_block_size(1000),
2016-07-08 07:41:30 -03:00
m_target_overlap(0.75),
m_window_type(window::DODGY),
m_target_window_type(window::DODGY)
2015-07-11 15:03:05 -03:00
{
2016-07-08 07:41:30 -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() {
2016-07-08 07:41:30 -03:00
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() {
2016-07-08 07:41:30 -03:00
command_ring_buffer::command cmd;
while(true) {
while (m_osc.get(cmd)) {
string name = cmd.m_name;
//cerr<<name<<endl;
if (name=="/load_sample") {
pthread_mutex_lock(m_brain_mutex);
m_source.load_sound(cmd.get_string(0),brain::MIX);
pthread_mutex_unlock(m_brain_mutex);
2016-07-08 07:41:30 -03:00
}
if (name=="/delete_sample") {
pthread_mutex_lock(m_brain_mutex);
m_source.delete_sound(cmd.get_string(0));
pthread_mutex_unlock(m_brain_mutex);
2016-07-08 07:41:30 -03:00
}
if (name=="/activate_sound") {
pthread_mutex_lock(m_brain_mutex);
m_source.activate_sound(cmd.get_string(0),1);
pthread_mutex_unlock(m_brain_mutex);
2016-07-08 07:41:30 -03:00
}
if (name=="/deactivate_sound") {
pthread_mutex_lock(m_brain_mutex);
m_source.activate_sound(cmd.get_string(0),0);
pthread_mutex_unlock(m_brain_mutex);
2016-07-08 07:41:30 -03:00
}
if (name=="/source_block_size") {
m_source_block_size = cmd.get_int(0);
2016-07-08 07:41:30 -03:00
}
if (name=="/source_overlap") {
m_source_overlap = m_source_block_size*cmd.get_float(0);
2016-07-08 07:41:30 -03:00
}
if (name=="/generate_brain") {
pthread_mutex_lock(m_brain_mutex);
m_source.init(m_source_block_size, m_source_overlap, m_window_type);
search_params p(1,0,0,100,0);
m_source.build_synapses_fixed(p);
m_left_renderer->reset();
m_right_renderer->reset();
pthread_mutex_unlock(m_brain_mutex);
2016-07-08 07:41:30 -03:00
}
if (name=="/load_target") {
pthread_mutex_lock(m_brain_mutex);
m_left_target.clear_sounds();
m_left_target.load_sound(cmd.get_string(0),brain::LEFT);
m_right_target.clear_sounds();
m_right_target.load_sound(cmd.get_string(0),brain::RIGHT);
pthread_mutex_unlock(m_brain_mutex);
2016-07-08 07:41:30 -03:00
}
if (name=="/target_block_size") {
m_target_block_size = cmd.get_int(0);
m_block_stream->init(m_target_block_size, m_target_overlap, m_target_window_type);
2016-07-08 07:41:30 -03:00
}
if (name=="/target_overlap") {
m_target_overlap = m_target_block_size*cmd.get_float(0);
m_block_stream->init(m_target_block_size, m_target_overlap, m_target_window_type);
2016-07-08 07:41:30 -03:00
}
if (name=="/generate_target") {
pthread_mutex_lock(m_brain_mutex);
m_left_target.init(m_target_block_size, m_target_overlap, m_target_window_type);
m_right_target.init(m_target_block_size, m_target_overlap, m_target_window_type);
// probably elsewhere
m_block_stream->init(m_target_block_size, m_target_overlap, m_target_window_type);
pthread_mutex_unlock(m_brain_mutex);
2016-07-08 07:41:30 -03:00
}
if (name=="/window_type") {
m_window_type=(window::type)cmd.get_int(0);
2016-07-08 07:41:30 -03:00
}
if (name=="/target_window_type") {
m_target_window_type=(window::type)cmd.get_int(0);
m_block_stream->init(m_target_block_size, m_target_overlap, m_target_window_type);
2016-07-08 07:41:30 -03:00
}
if (name=="/load_brain") {
load_source(cmd.get_string(0));
2016-07-08 07:41:30 -03:00
}
if (name=="/save_brain") {
save_source(cmd.get_string(0));
2016-07-08 07:41:30 -03:00
}
if (name=="/load_session") {
load_session(cmd.get_string(0));
2016-07-08 07:41:30 -03:00
}
if (name=="/save_session") {
save_session(cmd.get_string(0));
2016-07-08 07:41:30 -03:00
}
2022-12-05 06:16:05 -03:00
if (name=="/stereo") {
// only for session file save
m_stereo=cmd.get_int(0);
}
2015-07-11 15:03:05 -03:00
}
2016-09-24 06:41:10 -03:00
#ifdef WIN32
Sleep(1);
#else
2016-07-08 07:41:30 -03:00
usleep(500);
2016-09-24 06:41:10 -03:00
#endif
2016-07-08 07:41:30 -03:00
}
2015-07-11 15:03:05 -03:00
}
void process_thread::load_source(const std::string &filename) {
2016-07-08 07:41:30 -03:00
pthread_mutex_lock(m_brain_mutex);
m_source.clear();
ifstream ifs(filename.c_str(),ios::binary);
ifs||m_source;
ifs.close();
pthread_mutex_unlock(m_brain_mutex);
}
void process_thread::save_source(const std::string &filename) {
2016-07-08 07:41:30 -03:00
pthread_mutex_lock(m_brain_mutex);
ofstream ofs(filename.c_str(),ios::binary);
ofs||m_source;
ofs.close();
pthread_mutex_unlock(m_brain_mutex);
}
2016-02-03 13:45:09 -02:00
// remember to change GUI side to match in MainWindow.cpp
2015-10-16 12:23:51 -03:00
void process_thread::load_session(const std::string &filename) {
2016-07-08 07:41:30 -03:00
pthread_mutex_lock(m_brain_mutex);
m_source.clear();
m_left_target.clear();
m_right_target.clear();
ifstream ifs(filename.c_str(),ios::binary);
2022-12-05 06:16:05 -03:00
stream_session(ifs);
2016-07-08 07:41:30 -03:00
ifs.close();
pthread_mutex_unlock(m_brain_mutex);
2015-10-16 12:23:51 -03:00
}
void process_thread::save_session(const std::string &filename) {
2016-07-08 07:41:30 -03:00
pthread_mutex_lock(m_brain_mutex);
ofstream ofs(filename.c_str(),ios::binary);
2022-12-05 06:16:05 -03:00
stream_session(ofs);
2016-07-08 07:41:30 -03:00
ofs.close();
pthread_mutex_unlock(m_brain_mutex);
}
2022-12-05 06:16:05 -03:00
void process_thread::stream_session(std::ios &fs) {
u32 version=1;
fs||version;
fs||(*m_left_renderer);
fs||(*m_right_renderer);
fs||m_source_block_size||m_source_overlap;
fs||m_target_block_size||m_target_overlap;
fs||m_window_type||m_target_window_type;
fs||m_source;
fs||m_left_target;
fs||m_right_target;
if (version>0) {
fs||m_stereo;
}
}