serialisation

This commit is contained in:
Dave Griffiths 2015-08-03 09:18:20 +01:00
parent 089d8e9c20
commit 5426efa993
14 changed files with 355 additions and 7 deletions

View File

@ -16,7 +16,7 @@ SRCS := src/fft.cpp \
TARGET_SRCS := src/main.cpp
# @CFLAGS@
CCFLAGS = -Ofast -march=native -mtune=native -std=c++11 -ffast-math -Wno-unused -Isrc -I/opt/local/include
CCFLAGS = -g -Ofast -march=native -mtune=native -std=c++11 -ffast-math -Wno-unused -Isrc -I/opt/local/include
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@

View File

@ -0,0 +1,156 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="744.09448819"
height="1052.3622047"
id="svg2"
version="1.1"
inkscape:version="0.48.3.1 r9886"
sodipodi:docname="New document 1">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.98994949"
inkscape:cx="255.65918"
inkscape:cy="681.62441"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="true"
inkscape:window-width="814"
inkscape:window-height="690"
inkscape:window-x="525"
inkscape:window-y="129"
inkscape:window-maximized="0">
<inkscape:grid
type="xygrid"
id="grid2985" />
</sodipodi:namedview>
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<path
sodipodi:type="arc"
style="fill:#000000;fill-opacity:1;stroke:none"
id="path2987"
sodipodi:cx="225"
sodipodi:cy="427.36218"
sodipodi:rx="125"
sodipodi:ry="125"
d="m 350,427.36218 a 125,125 0 1 1 -250,0 125,125 0 1 1 250,0 z"
transform="translate(25,200)" />
<rect
style="fill:#e60000;fill-opacity:1;stroke:none"
id="rect3757"
width="150"
height="150"
x="100"
y="102.36218"
ry="50"
inkscape:export-filename="/home/dave/code/samplebrain/samplebrain/qt/images/record.png"
inkscape:export-xdpi="120"
inkscape:export-ydpi="120" />
<path
sodipodi:type="arc"
style="fill:#ffffff;fill-opacity:1;stroke:none"
id="path3759"
sodipodi:cx="175"
sodipodi:cy="177.36218"
sodipodi:rx="45"
sodipodi:ry="45"
d="m 220,177.36218 a 45,45 0 1 1 -90,0 45,45 0 1 1 90,0 z" />
<rect
style="fill:#000000;fill-opacity:1;stroke:none"
id="rect3757-0"
width="150"
height="150"
x="250"
y="102.36218"
ry="50"
inkscape:export-filename="/home/dave/code/samplebrain/samplebrain/qt/images/stop.png"
inkscape:export-xdpi="120"
inkscape:export-ydpi="120" />
<rect
style="fill:#ffffff;fill-opacity:1;stroke:none"
id="rect3779"
width="70"
height="70"
x="290"
y="142.36218"
ry="0" />
<rect
style="fill:#ffffff;fill-opacity:1;stroke:none"
id="rect3781"
width="30"
height="10"
x="400"
y="282.36218" />
<rect
style="fill:#00d02f;fill-opacity:1;stroke:none"
id="rect3757-8"
width="150"
height="150"
x="100"
y="252.36218"
ry="50"
inkscape:export-filename="/home/dave/code/samplebrain/samplebrain/qt/images/play.png"
inkscape:export-xdpi="120"
inkscape:export-ydpi="120" />
<rect
style="fill:#0b45f0;fill-opacity:1;stroke:none"
id="rect3757-0-2"
width="150"
height="150"
x="250"
y="252.36218"
ry="50"
inkscape:export-filename="/home/dave/code/samplebrain/samplebrain/qt/images/pause.png"
inkscape:export-xdpi="120"
inkscape:export-ydpi="120" />
<path
style="fill:#ffffff;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;fill-opacity:1"
d="m 140,372.36218 0,-90 80,50 z"
id="path3810"
inkscape:connector-curvature="0" />
<rect
style="fill:#ffffff;fill-opacity:1;stroke:none"
id="rect3812"
width="30"
height="70"
x="290"
y="292.36218" />
<rect
style="fill:#ffffff;fill-opacity:1;stroke:none"
id="rect3812-5"
width="30"
height="70"
x="330"
y="292.36218" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -180,8 +180,24 @@ double block::compare(const block &other, const search_params &params) const {
other.m_usage, params.m_usage_importance);
}
ios &spiralcore::operator||(ios &s, block &b) {
u32 version=1;
string id("block");
s||id||version;
s||b.m_pcm||b.m_fft||b.m_mfcc;
s||b.m_n_pcm||b.m_n_fft||b.m_n_mfcc;
s||b.m_block_size||b.m_rate||b.m_orig_filename;
stream_vector(s,b.m_synapse);
return s;
}
bool block::unit_test() {
stream_unit_test();
sample ntest(3);
u32 idx=0;
ntest[idx++]=-1;
@ -228,6 +244,25 @@ bool block::unit_test() {
assert(bb.m_rate==44100);
assert(bb.m_block_size==data.get_length());
ofstream of("test_data/blocktest.bin",ios::binary);
of||bb;
of.close();
cerr<<"written"<<endl;
ifstream ifs("test_data/blocktest.bin",ios::binary);
block bbb;
ifs||bbb;
ifs.close();
assert(bbb.m_pcm.get_length()==data.get_length());
//assert(bb.m_fft.get_length()==data.get_length());
assert(bbb.m_mfcc.get_length()==MFCC_FILTERS);
assert(bbb.m_orig_filename==string("test"));
assert(bbb.m_rate==44100);
assert(bbb.m_block_size==data.get_length());
search_params p(0,0,0,100,0);
block bb2("test",data,44100,w);
assert(bb.compare(bb2,p)==0);

View File

@ -22,6 +22,7 @@
#include "mfcc.h"
#include "search_params.h"
#include "window.h"
#include "jellyfish/core/stream.h"
#ifndef BLOCK
#define BLOCK
@ -32,6 +33,7 @@ class block {
public:
// runs analysis on pcm
block(const std::string &filename, const sample &pcm, u32 rate, const window &w, bool ditchpcm=false);
block() {}
// returns distance based on ratio of fft-mfcc values
double compare(const block &other, const search_params &params) const;
@ -71,8 +73,12 @@ private:
std::vector<u32> m_synapse;
float m_usage;
friend ios &operator||(ios &s, block &b);
};
ios &operator||(ios &s, block &b);
}
#endif

View File

@ -223,6 +223,23 @@ void brain::deplete_usage() {
}
*/
ios &spiralcore::operator||(ios &s, brain::sound &b) {
u32 version=0;
string id("brain::sound");
s||id||version;
s||b.m_filename||b.m_sample;
}
ios &spiralcore::operator||(ios &s, brain &b) {
u32 version=0;
string id("brain");
s||id||version;
s||b.m_blocks;
stream_list(s,b.m_samples);
s||b.m_block_size||b.m_overlap||b.m_window;
s||b.m_current_block_index||b.m_average_error||b.m_usage_falloff;
}
bool brain::unit_test() {
brain b;
assert(b.m_samples.size()==0);
@ -258,6 +275,24 @@ bool brain::unit_test() {
assert(b3.search(b2.m_blocks[19],p)==19);
assert(b3.search(b2.m_blocks[29],p)==29);
ofstream of("test_data/test.brain",ios::binary);
of||b3;
of.close();
brain b4;
ifstream ifs("test_data/test.brain",ios::binary);
ifs||b4;
ifs.close();
assert(b4.search(b2.m_blocks[0],p)==0);
assert(b4.search(b2.m_blocks[9],p)==9);
assert(b4.search(b2.m_blocks[19],p)==19);
assert(b4.search(b2.m_blocks[29],p)==29);
cerr<<"!!!"<<endl;
// sample r = b2.resynth(b,1);
// assert(r.get_length()==200);

View File

@ -40,6 +40,8 @@ public:
sound(const std::string &name, const sample &sample) :
m_filename(name), m_sample(sample) {}
sound() {}; // needed for streaming
std::string m_filename;
sample m_sample;
};
@ -74,6 +76,8 @@ public:
static bool unit_test();
friend ios &operator||(ios &s, brain &b);
private:
void chop_and_add(const sample &s, u32 count, bool ditchpcm=false);
@ -93,6 +97,9 @@ private:
float m_usage_falloff;
};
ios &operator||(ios &s, brain::sound &b);
ios &operator||(ios &s, brain &b);
}
#endif

View File

@ -108,3 +108,25 @@ void window::run(const sample &input) const {
input[n]*=(*m_windows[m_current_type])[n];
}
}
ios &spiralcore::operator||(ios &s, window &b) {
u32 version=0;
string id("window");
s||id||version;
s||b.m_current_type;
std::ofstream *pos=dynamic_cast<std::ofstream*>(&s);
if (pos!=NULL) {
u32 size=0;
// get the size from the first window
if (b.m_windows.size()>0) {
size=b.m_windows[0]->get_length();
}
s||size;
} else {
u32 size=0;
s||size;
// reinit using size if we are reading in
b.init(size);
}
return s;
}

View File

@ -44,6 +44,8 @@ public:
void set_current_type(type t) { m_current_type=t; }
void run(const sample &sample) const;
friend ios &operator||(ios &s, window &b);
private:
void clear();
@ -53,6 +55,8 @@ private:
};
ios &operator||(ios &s, window &b);
}
#endif

Binary file not shown.

Binary file not shown.

View File

@ -4,6 +4,9 @@
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
class desc {
@ -54,9 +57,89 @@ public:
}
};
template<typename T>ios &operator||(ios &s, T &v) {
ofstream *pos=dynamic_cast<ofstream*>(&s);
if (pos!=NULL) {
ofstream &os = *pos;
return os.write((char*)(&v),sizeof(T));
}
else
{
ifstream *pis=dynamic_cast<ifstream*>(&s);
assert(pis);
ifstream &is = *pis;
return is.read((char *)(&v),sizeof(T));
}
}
template<typename T>ios &stream_array(ios &s, T *v, size_t &len) {
ofstream *pos=dynamic_cast<ofstream*>(&s);
if (pos!=NULL) {
ofstream &os = *pos;
os||len;
return os.write((char*)v,sizeof(T)*len);
}
else
{
ifstream *pis=dynamic_cast<ifstream*>(&s);
assert(pis);
ifstream &is = *pis;
is||len;
return is.read((char *)v,sizeof(T)*len);
}
}
class sertest {
public:
sertest() {
size = 100;
buf = new float[size];
}
void build() {
for (unsigned int n=0; n<size; n++) {
buf[n]=n;
}
}
float a;
int c;
float *buf;
size_t size;
};
bool operator||(ios &s, sertest &v) {
s||v.a||v.c;
stream_array(s,v.buf,v.size);
}
int main() {
auto func = [] () { cout << "Hello world"; };
func(); // now call the function
sertest ss;
ss.a=100;
ss.c=230;
ss.build();
ofstream of("test.bin", ios::binary);
of||ss;
of.close();
sertest sb;
ifstream inf("test.bin", ios::binary);
inf||sb;
inf.close();
cerr<<sb.a<<" "<<sb.c<<" "<<sb.buf[50]<<endl;
// auto func = [] () { cout << "Hello world"; };
// func(); // now call the function
/* test t=test(999);
desc d;