diff --git a/src/diskreader.cxx b/src/diskreader.cxx index 7dd0ac0..ac324b4 100644 --- a/src/diskreader.cxx +++ b/src/diskreader.cxx @@ -164,48 +164,53 @@ int DiskReader::loadSample( int track, int scene, string path ) { /// load the sample SndfileHandle infile( path, SFM_READ ); - std::vector buf( infile.frames() * infile.channels() ); - infile.read( (float*)&buf[0] , infile.frames() * infile.channels() ); + int chnls = infile.channels(); + std::vector bufL( infile.frames() / chnls ); + std::vector bufR( infile.frames() / chnls ); + std::vector bufTmp( infile.frames() * (chnls-2) ); if ( infile.error() ) { LUPPP_ERROR("File %s, Error %s", path.c_str(), infile.strError() ); return LUPPP_RETURN_ERROR; } - LUPPP_NOTE("Loading file with %i chnls, frames %i,buffer size %i", infile.channels(), infile.frames(), buf.size() ); + LUPPP_NOTE("Loading file with %i chnls, frames %i, bufferL size %i bufferR size %i", infile.channels(), infile.frames(), bufL.size(), bufR.size() ); - /// kick stereo channel? - int chnls = infile.channels(); - if ( chnls > 1 ) { - // we're gonna kick all samples that are *not* channel 1 - std::vector tmp( buf.size() / chnls ); + // Read data + for(int f=0; fsamplerate ) { LUPPP_NOTE("%s%i%s%i", "Resampling from ", infile.samplerate(), " to ", gui->samplerate); float resampleRatio = float(gui->samplerate) / infile.samplerate(); - std::vector resampled( infile.frames() * resampleRatio ); + std::vector resampledL( infile.frames() / chnls * resampleRatio ); + std::vector resampledR( infile.frames() / chnls * resampleRatio ); - SRC_DATA data; - data.data_in = &buf[0]; - data.data_out = &resampled[0]; + SRC_DATA dataL; + SRC_DATA dataR; + dataL.data_in = &bufL[0]; + dataR.data_in = &bufR[0]; + dataL.data_out = &resampledL[0]; + dataR.data_out = &resampledR[0]; - data.input_frames = infile.frames(); - data.output_frames = infile.frames() * resampleRatio; + dataL.input_frames = infile.frames() / chnls; + dataR.input_frames = infile.frames() / chnls; + dataL.output_frames = infile.frames() / chnls * resampleRatio; + dataR.output_frames = infile.frames() / chnls * resampleRatio; - data.end_of_input = 0; - data.src_ratio = resampleRatio; + dataL.end_of_input = 0; + dataR.end_of_input = 0; + dataL.src_ratio = resampleRatio; + dataR.src_ratio = resampleRatio; int q = SRC_SINC_FASTEST; @@ -223,22 +228,27 @@ int DiskReader::loadSample( int track, int scene, string path ) // resample quality taken from config file, - int ret = src_simple ( &data, q, 1 ); + int ret = src_simple ( &dataL, q, 1 ); if ( ret == 0 ) - LUPPP_NOTE("%s%i%s%i", "Resampling finished, from ", data.input_frames_used, " to ", data.output_frames_gen ); + LUPPP_NOTE("%s%i%s%i", "Resampling L finished, from ", dataL.input_frames_used, " to ", dataL.output_frames_gen ); else - LUPPP_ERROR("%s%i%s%i", "Resampling finished, from ", data.input_frames_used, " to ", data.output_frames_gen ); + LUPPP_ERROR("%s%i%s%i", "Resampling L finished, from ", dataL.input_frames_used, " to ", dataL.output_frames_gen ); + ret = src_simple ( &dataR, q, 1 ); + if ( ret == 0 ) + LUPPP_NOTE("%s%i%s%i", "Resampling R finished, from ", dataR.input_frames_used, " to ", dataR.output_frames_gen ); + else + LUPPP_ERROR("%s%i%s%i", "Resampling R finished, from ", dataR.input_frames_used, " to ", dataR.output_frames_gen ); /// exchange buffers, so buf contains the resampled audio - buf.swap( resampled ); + bufL.swap( resampledL ); + bufR.swap( resampledR ); } /// create buffer, and set the data AudioBuffer* ab = new AudioBuffer(); - ab->setAudioFrames( buf.size() ); - // TODO right (stereo) - ab->nonRtSetSample( buf, buf ); + ab->setAudioFrames( bufL.size() ); + ab->nonRtSetSample( bufL, bufR ); //cout << "DiskReader::loadSample() " << path << endl; diff --git a/src/diskwriter.cxx b/src/diskwriter.cxx index edb62e2..1d0bf27 100644 --- a/src/diskwriter.cxx +++ b/src/diskwriter.cxx @@ -316,13 +316,16 @@ int DiskWriter::writeAudioBuffer(int track, int scene, AudioBuffer* ab, } - SndfileHandle outfile( path.str(), SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 1, gui->samplerate ); + SndfileHandle outfile( path.str(), SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 2, gui->samplerate ); // FIXME: the size of the buffer is bigger than the audio contained in it: // calculate the length that needs saving using getBeats() * framesPerBeat - if ( ab->getAudioFrames() > 0 ) - // TODO right (stereo) - outfile.write( &ab->getDataL()[0], ab->getAudioFrames() ); + if ( ab->getAudioFrames() > 0 ) { + for(int i=0; igetAudioFrames(); i++) { + outfile.writef( &ab->getDataL()[i], 1); //sizeof(ab->getDataL()[i])); + outfile.writef( &ab->getDataR()[i], 1); //sizeof(ab->getDataR()[i])); + } + } else { LUPPP_WARN("%s","Sample has zero samples"); }