Commit fe9e0b54 authored by Paul Bethge's avatar Paul Bethge
Browse files

write wav for check

parent c78e5d3c
......@@ -15,11 +15,12 @@
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofSetWindowTitle("example_keyword_spotting");
ofSetWindowTitle("ofxLID");
ofSetCircleResolution(80);
ofBackground(54, 54, 54);
......
......@@ -21,6 +21,10 @@
#include <deque>
#include <iostream>
#include "wav_file_writer_beta.h"
#include "ofFileUtils.h"
// a simple Fifo with adjustable max length
template <typename T, typename Container=std::deque<T>>
......@@ -60,9 +64,18 @@ class AudioClassifier : public ofxTF2::Model {
// downsample and empty the incoming Fifo
downsample(bufferFifo, sample, downsamplingFactor);
ofLog() << std::to_string(sample[0]);
normalize(sample);
ofLog() << std::to_string(sample[0]);
#if 1
std::string file_name(ofToDataPath("test.wav"));
sakado::WavFileWriterBeta wfw(file_name, 16000, 2, sample.size());
int16_t buf;
for (int i = 0; i < sample.size(); i++) {
buf = sample[i] *255*100; // scale data to int16 range
wfw.Write(&buf, 2, 1);
}
wfw.Close();
#endif
// convert recorded sample to a batch of size one
ofxTF2::shapeVector tensorShape {1, static_cast<ofxTF2::shape_t>(sample.size()), 1};
......
#ifdef SAKADO_WAV_FILE_WRITER_BETA_H
#else
#define SAKADO_WAV_FILE_WRITER_BETA_H
#ifdef _MSC_VER
#pragma warning(disable:4996)
#endif
#include<cstdio>
#include<string>
using namespace::std;
namespace sakado {
class WavFileWriterBeta {
FILE*fp;
public:
unsigned long FmtSize;
unsigned short FmtID;
unsigned short NumChannels;
unsigned long SampleRate;
unsigned long BytesPerSec;
unsigned short BlockAlign;//(bytes per sample)*(channels)
unsigned short BitsPerSample;
unsigned long DataSize;
unsigned short BytesPerSample;
unsigned long NumData;
string Filename;
WavFileWriterBeta(string filename, unsigned long SampleRate, unsigned short BytesPerSample, unsigned long NumData) {
char riff[] = "RIFF";
char wave[] = "WAVE";
char fmt[] = "fmt ";
char data[] = "data";
unsigned long bufl;
unsigned long bufs;
this->Filename = filename;
fp = fopen(filename.c_str(), "wb");
fwrite(riff, 1, 4, fp);
bufl = 36 + (NumData * BytesPerSample);
fwrite(&bufl, 1, 4, fp);
fwrite(wave, 1, 4, fp);
fwrite(fmt, 1, 4, fp);
bufl = 16;
fwrite(&bufl, 1, 4, fp);
bufs = 1;
fwrite(&bufs, 1, 2, fp);
fwrite(&bufs, 1, 2, fp);
bufl = SampleRate;
fwrite(&bufl, 1, 4, fp);
bufl = BytesPerSample * SampleRate;
fwrite(&bufl, 1, 4, fp);
bufs = BytesPerSample;
fwrite(&bufs, 1, 2, fp);
bufs = BytesPerSample * 8;
fwrite(&bufs, 1, 2, fp);
fwrite(data, 1, 4, fp);
bufl = NumData * BytesPerSample;
fwrite(&bufl, 1, 4, fp);
}
~WavFileWriterBeta() {
if(fp != NULL)fclose(fp);
}
unsigned int Write(const void* buf, size_t elementSize, size_t elementCount) {
return fwrite(buf, elementSize, elementCount, fp);
}
void Close() {
if(fp != NULL)fclose(fp);
fp = NULL;
}
//Not yet tested
void Reopen() {
if(fp == NULL)fp = fopen(Filename.c_str(), "ab");
}
};
}
#endif
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment