static void create_file (const char * fname, int format) { static short buffer [BUFFER_LEN] ; SndfileHandle file ; int channels = 2 ; int srate = 48000 ; printf ("Creating file named '%s'\n", fname) ; file = SndfileHandle (fname, SFM_WRITE, format, channels, srate) ; memset (buffer, 0, sizeof (buffer)) ; const int size = srate*3; float sample[size]; float current =0; for(int i =0; i<size;i++) sample[i] = sin(float(i)/size*M_PI*1500); file.write (&sample[0], size) ; puts ("") ; /* ** The SndfileHandle object will automatically close the file and ** release all allocated memory when the object goes out of scope. ** This is the Resource Acquisition Is Initailization idom. ** See : http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization */ } /* create_file */
void CSignalRecorder::StoreCapturedSamples(SndfileHandle &CaptureWave) { ulong capturedSamples = 0; do { capturedSamples = m_CaptureDevice->GetSamples(m_SamplesBuffer->get_Frame(0), m_FrameSize*m_MaxCaptureChannels); capturedSamples /= m_MaxCaptureChannels; if(capturedSamples > 0) { ulong writtenSamples = 0; if(m_CaptureDevice->get_InputChannelAmount() > 1) { writtenSamples = (ulong)CaptureWave.writef(m_SamplesBuffer->get_Frame(0), capturedSamples); } else { writtenSamples = (ulong)CaptureWave.write(m_SamplesBuffer->get_Frame(0), capturedSamples); } if(writtenSamples < capturedSamples) { KODI->Log(LOG_ERROR, "Failed to write to capture wave file!"); Set_State(STATE_INVALID); } } }while(capturedSamples > 0); }
void AudioManager::testCropping (int sId, int gId) { Sample cropped = getGroundTruthAround(sId, gId); SF_INFO sfinfo; sfinfo.channels = 1; sfinfo.samplerate = FS; sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; string file = "output/cropped.wav"; cout << "Saving " << file << endl; SndfileHandle ofile = SndfileHandle(file, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 1, 8000); ofile.write(cropped.audio, cropped.length); }
void AudioManager::saveSamples () { SF_INFO sfinfo; sfinfo.channels = 1; sfinfo.samplerate = FS; sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; for (vector<Sample>::iterator it = samples.begin(); it != samples.end(); ++it) { string file = "output/" + to_string(it->offset) + "-" + it->station + ".wav"; cout << "Saving " << file << endl; SndfileHandle ofile = SndfileHandle(file, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 1, 8000); ofile.write(it->audio, it->length); //sf_write_sync(ofile); //sf_close(ofile); } }