samplebrain/brain/src/brain.h

127 lines
3.9 KiB
C
Raw Normal View History

2015-07-21 10:58:13 -03:00
// Copyright (C) 2015 Foam Kernow
//
// 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 17:30:16 -03:00
#include <list>
2015-07-08 04:08:49 -03:00
#include <vector>
2015-07-08 06:24:02 -03:00
#include <string>
2022-09-04 07:38:20 -03:00
#include "spiralcore/types.h"
#include "spiralcore/sample.h"
2016-07-08 07:41:30 -03:00
#include "block_source.h"
2015-07-09 17:48:59 -03:00
#include "block.h"
2015-07-11 08:29:51 -03:00
#include "search_params.h"
2015-07-18 20:34:56 -03:00
#include "window.h"
2015-07-08 06:24:02 -03:00
#ifndef BRAIN
#define BRAIN
namespace spiralcore {
2015-07-08 04:08:49 -03:00
2016-07-08 07:41:30 -03:00
class brain : public block_source {
2015-07-08 04:08:49 -03:00
public:
brain();
2016-02-03 13:45:09 -02:00
enum stereo_mode { MIX, LEFT, RIGHT };
2015-07-08 04:08:49 -03:00
// rewrites whole brain
2015-07-18 20:34:56 -03:00
void init(u32 block_size, u32 overlap, window::type t, bool ditchpcm=false);
2015-07-08 04:08:49 -03:00
2015-10-16 12:23:51 -03:00
void clear();
2015-07-08 06:24:02 -03:00
// load, chop up and add to brain
// todo: add tags
2016-02-03 13:45:09 -02:00
void load_sound(std::string filename, stereo_mode mode);
2015-07-11 17:30:16 -03:00
void delete_sound(std::string filename);
void clear_sounds() { m_samples.clear(); }
2016-02-25 09:47:38 -03:00
// turns on/off a sound in realtime without reprocessing
void activate_sound(std::string filename, bool active);
2015-07-08 06:24:02 -03:00
2015-07-08 11:06:08 -03:00
const sample &get_block_pcm(u32 index) const;
const sample &get_block_n_pcm(u32 index) const;
2016-07-08 07:41:30 -03:00
virtual const block &get_block(u32 index) const;
virtual u32 get_num_blocks() const { return m_blocks.size(); }
2015-07-09 12:16:36 -03:00
void set_usage_falloff(float s) { m_usage_falloff=s; }
2015-10-16 12:23:51 -03:00
float get_usage_falloff() { return m_usage_falloff; }
// basic search
u32 search(const block &target, const search_params &params);
u32 rev_search(const block &target, const search_params &params);
// synaptic search
double calc_average_diff(search_params &params);
2015-08-04 06:14:06 -03:00
void build_synapses_thresh(search_params &params, double threshold);
void build_synapses_fixed(search_params &params);
u32 search_synapses(const block &target, search_params &params);
2015-08-05 18:09:08 -03:00
double get_current_error() { return m_current_error; }
2016-02-25 09:47:38 -03:00
// randomise the synaptic pointer
void jiggle();
2015-07-09 12:16:36 -03:00
2015-07-08 06:24:02 -03:00
static bool unit_test();
2015-07-08 04:08:49 -03:00
2016-02-25 09:47:38 -03:00
class sound {
public:
sound(const std::string &name, const sample &sample) :
2016-02-26 11:05:15 -03:00
m_filename(name), m_sample(sample), m_num_blocks(0),
m_enabled(true), m_start(0), m_end(0) {}
2016-02-25 09:47:38 -03:00
sound() {}; // needed for streaming
std::string m_filename;
sample m_sample;
// filled in during chop_and_add
u32 m_num_blocks;
2016-02-26 11:05:15 -03:00
bool m_enabled;
// we store the blocks separately in a flat list, so we can
// directly index them, but we need to filter them by source
// sound - so we need to be able to turn large contiguous sets
// of them on and off (without a big impact on the processing
// time)
u32 m_start,m_end;
2016-02-25 09:47:38 -03:00
};
const std::list<sound> &get_samples() { return m_samples; }
2015-08-03 05:18:20 -03:00
2015-07-08 04:08:49 -03:00
private:
2016-02-25 09:47:38 -03:00
void chop_and_add(sound &s, u32 count, bool ditchpcm=false);
void deplete_usage();
u32 stickify(const block &target, u32 closest_index, f32 dist, const search_params &params);
2016-02-25 09:47:38 -03:00
void recompute_sample_sections();
// checks sample sections
bool is_block_active(u32 index);
2015-07-09 17:48:59 -03:00
vector<block> m_blocks;
2015-07-11 17:30:16 -03:00
std::list<sound> m_samples;
2015-09-23 14:35:13 -03:00
vector<string> m_active_sounds;
2015-07-08 04:08:49 -03:00
2015-07-18 20:34:56 -03:00
window m_window;
u32 m_current_block_index;
double m_current_error;
double m_average_error;
float m_usage_falloff;
2016-02-25 09:47:38 -03:00
friend ios &operator||(ios &s, brain &b);
friend ios &operator||(ios &s, sound &b);
2015-07-08 04:08:49 -03:00
};
2015-07-08 06:24:02 -03:00
2015-08-03 05:18:20 -03:00
ios &operator||(ios &s, brain::sound &b);
ios &operator||(ios &s, brain &b);
2015-07-08 06:24:02 -03:00
}
2015-07-09 17:48:59 -03:00
#endif