//-------------------------------------------------------------- bool ofxSoundFile::save(string path, const ofSoundBuffer &buff){ // check that we're writing a wav and complain if the file extension is wrong. ofFile f(path); if(ofToLower(f.getExtension())!="wav") { path += ".wav"; ofLogWarning() << "Can only write wav files - will save file as " << path; } fstream file(ofToDataPath(path).c_str(), ios::out | ios::binary); if(!file.is_open()) { ofLogError() << "Error opening sound file '" << path << "' for writing"; return false; } // write a wav header short myFormat = 1; // for pcm int mySubChunk1Size = 16; int bitsPerSample = 16; // assume 16 bit pcm int myByteRate = buff.getSampleRate() * buff.getNumChannels() * bitsPerSample/8; short myBlockAlign = buff.getNumChannels() * bitsPerSample/8; int myChunkSize = 36 + buff.size()*bitsPerSample/8; int myDataSize = buff.size()*bitsPerSample/8; int channels = buff.getNumChannels(); int samplerate = buff.getSampleRate(); file.seekp (0, ios::beg); file.write ("RIFF", 4); file.write ((char*) &myChunkSize, 4); file.write ("WAVE", 4); file.write ("fmt ", 4); file.write ((char*) &mySubChunk1Size, 4); file.write ((char*) &myFormat, 2); // should be 1 for PCM file.write ((char*) &channels, 2); // # channels (1 or 2) file.write ((char*) &samplerate, 4); // 44100 file.write ((char*) &myByteRate, 4); // file.write ((char*) &myBlockAlign, 2); file.write ((char*) &bitsPerSample, 2); //16 file.write ("data", 4); file.write ((char*) &myDataSize, 4); // write the wav file per the wav file format, 4096 bytes of data at a time. #define WRITE_BUFF_SIZE 4096 short writeBuff[WRITE_BUFF_SIZE]; int pos = 0; while(pos<buff.size()) { int len = MIN(WRITE_BUFF_SIZE, buff.size()-pos); for(int i = 0; i < len; i++) { writeBuff[i] = (int)(buff[pos]*32767.f); pos++; } file.write((char*)writeBuff, len*bitsPerSample/8); } file.close(); return true; }
void ofxBasicSoundPlayer::audioOut(ofSoundBuffer& outputBuffer){ if(bIsPlaying){ int nFrames = outputBuffer.getNumFrames(); int nChannels = outputBuffer.getNumChannels(); if (playerNumChannels != nChannels || playerNumFrames != nFrames || playerSampleRate != outputBuffer.getSampleRate()) { audioOutBuffersChanged(nFrames, nChannels, outputBuffer.getSampleRate()); } if(streaming){ int samplesRead = soundFile.readTo(buffer,nFrames); if ( samplesRead==0 ){ bIsPlaying=false; soundFile.seekTo(0); } else{ buffer.stereoPan(volumesLeft.back(),volumesRight.back()); newBufferE.notify(this,buffer); buffer.copyTo(outputBuffer); } }else{ if (positions.size() == 1 && abs(speed - 1)<FLT_EPSILON) { buffer.copyTo(outputBuffer,nFrames,nChannels,positions[0],loop); }else{ for(int i=0;i<(int)positions.size();i++){ //assert( resampledBuffer.getNumFrames() == bufferSize*relativeSpeed[i] ); if(abs(relativeSpeed[i] - 1)<FLT_EPSILON){ buffer.copyTo(resampledBuffer,nFrames,nChannels,positions[i],loop); }else{ buffer.resampleTo(resampledBuffer,positions[i],nFrames,relativeSpeed[i],loop, ofSoundBuffer::Linear); } resampledBuffer.stereoPan(volumesLeft[i],volumesRight[i]); newBufferE.notify(this,resampledBuffer); resampledBuffer.addTo(outputBuffer,0,loop); } } updatePositions(nFrames); } } }