zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit 2379f29a91749c350c537464fef6669da5b955d1
parent c7630b20ee8d0eca3769ae365cb3a88847f47f0c
Author: fundamental <[email protected]>
Date:   Fri,  1 Jan 2010 13:42:34 -0500

WavEngine: cleanup

Diffstat:
Msrc/Misc/Recorder.cpp | 2--
Msrc/Misc/WavFile.h | 2++
Msrc/Nio/WavEngine.cpp | 80+++++--------------------------------------------------------------------------
Msrc/Nio/WavEngine.h | 18+++++-------------
4 files changed, 12 insertions(+), 90 deletions(-)

diff --git a/src/Misc/Recorder.cpp b/src/Misc/Recorder.cpp @@ -65,7 +65,6 @@ void Recorder::stop() { sysOut->remove(wave); wave->Stop(); - wave->Close(); delete wave; wave = NULL; //is this even needed? } @@ -92,7 +91,6 @@ void Recorder::triggernow() if(status == 2) { if(notetrigger!=1) { wave->openAudio(); - //wave->Start(); sysOut->add(wave); } notetrigger = 1; diff --git a/src/Misc/WavFile.h b/src/Misc/WavFile.h @@ -1,5 +1,7 @@ /* + ZynAddSubFX - a software synthesizer + WavFile.h - Records sound to a file Copyright (C) 2008 Nasca Octavian Paul Author: Nasca Octavian Paul Mark McCurry diff --git a/src/Nio/WavEngine.cpp b/src/Nio/WavEngine.cpp @@ -23,32 +23,19 @@ using namespace std; -WavEngine::WavEngine(OutMgr *out, string _filename, int _samplerate, int _channels) - :AudioOut(out),filename(_filename),sampleswritten(0), - samplerate(_samplerate),channels(_channels),file(NULL) +WavEngine::WavEngine(OutMgr *out, string filename, int samplerate, int channels) + :AudioOut(out), file(filename, samplerate, channels) { - pthread_mutex_init(&write_mutex, NULL); - pthread_cond_init(&stop_cond, NULL); } - WavEngine::~WavEngine() { -#warning TODO cleanup mutex - Close(); + Stop(); } bool WavEngine::openAudio() { - file = fopen(filename.c_str(), "w"); - if(!file) - return false; - this->samplerate = samplerate; - this->channels = channels; - sampleswritten = 0; - char tmp[44]; - fwrite(tmp, 1, 44, file); - return true; + return file.good(); } bool WavEngine::Start() @@ -81,50 +68,9 @@ void WavEngine::Stop() pthread_join(pThread, NULL); } -void WavEngine::Close() -{ - //Stop(); - - pthread_mutex_lock(&write_mutex); - if(file) { - unsigned int chunksize; - rewind(file); - - fwrite("RIFF", 4, 1, file); - chunksize = sampleswritten * 4 + 36; - fwrite(&chunksize, 4, 1, file); - - fwrite("WAVEfmt ", 8, 1, file); - chunksize = 16; - fwrite(&chunksize, 4, 1, file); - unsigned short int formattag = 1; //uncompresed wave - fwrite(&formattag, 2, 1, file); - unsigned short int nchannels = channels; //stereo - fwrite(&nchannels, 2, 1, file); - unsigned int samplerate_ = samplerate; //samplerate - fwrite(&samplerate_, 4, 1, file); - unsigned int bytespersec = samplerate * 2 * channels; //bytes/sec - fwrite(&bytespersec, 4, 1, file); - unsigned short int blockalign = 2 * channels; //2 channels * 16 bits/8 - fwrite(&blockalign, 2, 1, file); - unsigned short int bitspersample = 16; - fwrite(&bitspersample, 2, 1, file); - - fwrite("data", 4, 1, file); - chunksize = sampleswritten * blockalign; - fwrite(&chunksize, 4, 1, file); - - fclose(file); - file = NULL; - } - pthread_mutex_unlock(&write_mutex); - -} - //lazy getter const Stereo<Sample> WavEngine::getNext() { - //TODO make this write remaining sample to output when stopped. Stereo<Sample> ans; pthread_mutex_lock(&outBuf_mutex); bool isEmpty = outBuf.empty(); @@ -166,24 +112,8 @@ void *WavEngine::AudioThread() recordbuf_16bit[i*2] = limit((int)(smps.l()[i] * 32767.0), -32768, 32767); recordbuf_16bit[i*2+1] = limit((int)(smps.r()[i] * 32767.0), -32768, 32767); } - write_stereo_samples(size, recordbuf_16bit); + file.writeStereoSamples(size, recordbuf_16bit); } pthread_exit(NULL); } -void WavEngine::write_stereo_samples(int nsmps, short int *smps) -{ - pthread_mutex_lock(&write_mutex); - if(file) - fwrite(smps, nsmps, 4, file); - pthread_mutex_unlock(&write_mutex); - sampleswritten += nsmps; -} - -void WavEngine::write_mono_samples(int nsmps, short int *smps) -{ - if(file) - fwrite(smps, nsmps, 2, file); - sampleswritten += nsmps; -} - diff --git a/src/Nio/WavEngine.h b/src/Nio/WavEngine.h @@ -1,5 +1,7 @@ /* + ZynAddSubFX - a software synthesizer + WavEngine.h - Records sound to a file Copyright (C) 2008 Nasca Octavian Paul Author: Nasca Octavian Paul Mark McCurry @@ -21,18 +23,18 @@ #ifndef WAVENGINE_H #define WAVENGINE_H #include "AudioOut.h" +#include "../Misc/WavFile.h" #include <string> class WavEngine: public AudioOut { public: - WavEngine(OutMgr *out, std::string _filename, int _samplerate, int _channels); + WavEngine(OutMgr *out, std::string filename, int samplerate, int channels); ~WavEngine(); bool openAudio(); bool Start(); void Stop(); - void Close(); const Stereo<Sample> getNext(); @@ -41,17 +43,7 @@ class WavEngine: public AudioOut static void *_AudioThread(void *arg); private: - void write_stereo_samples(int nsmps, short int *smps); - void write_mono_samples(int nsmps, short int *smps); - std::string filename; - int sampleswritten; - int samplerate; - int channels; - FILE *file; - //pthread_mutex_t run_mutex; - pthread_mutex_t write_mutex; - pthread_mutex_t stop_mutex; - pthread_cond_t stop_cond; + WavFile file; }; #endif