mirror of
https://github.com/vale981/openAV-Luppp
synced 2025-03-05 09:01:39 -05:00
Read and write stereo files
This commit is contained in:
parent
f36e9b6bd0
commit
6a6431242f
2 changed files with 49 additions and 36 deletions
|
@ -164,48 +164,53 @@ int DiskReader::loadSample( int track, int scene, string path )
|
||||||
{
|
{
|
||||||
/// load the sample
|
/// load the sample
|
||||||
SndfileHandle infile( path, SFM_READ );
|
SndfileHandle infile( path, SFM_READ );
|
||||||
std::vector<float> buf( infile.frames() * infile.channels() );
|
int chnls = infile.channels();
|
||||||
infile.read( (float*)&buf[0] , infile.frames() * infile.channels() );
|
std::vector<float> bufL( infile.frames() / chnls );
|
||||||
|
std::vector<float> bufR( infile.frames() / chnls );
|
||||||
|
std::vector<float> bufTmp( infile.frames() * (chnls-2) );
|
||||||
|
|
||||||
if ( infile.error() ) {
|
if ( infile.error() ) {
|
||||||
LUPPP_ERROR("File %s, Error %s", path.c_str(), infile.strError() );
|
LUPPP_ERROR("File %s, Error %s", path.c_str(), infile.strError() );
|
||||||
return LUPPP_RETURN_ERROR;
|
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?
|
// Read data
|
||||||
int chnls = infile.channels();
|
for(int f=0; f<infile.frames()/chnls; f++) {
|
||||||
if ( chnls > 1 ) {
|
infile.read( (float*)&bufL[f] , 1 );
|
||||||
// we're gonna kick all samples that are *not* channel 1
|
infile.read( (float*)&bufR[f] , 1 );
|
||||||
std::vector<float> tmp( buf.size() / chnls );
|
|
||||||
|
|
||||||
LUPPP_NOTE("Non mono file: %i chnls found, old size %i, new size %i ", infile.channels(), buf.size(), tmp.size() );
|
// Read (and ignore) remaining channels
|
||||||
|
for(int c=0; c<chnls-2; c++) {
|
||||||
for(size_t i = 0; i < tmp.size(); i++ ) {
|
infile.read( (float*)&bufTmp[f], 1);
|
||||||
tmp.at(i) = buf.at( i * chnls );
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// swap the buffers
|
|
||||||
buf.swap( tmp );
|
|
||||||
}
|
|
||||||
|
|
||||||
/// resample?
|
/// resample?
|
||||||
if ( infile.samplerate() != gui->samplerate ) {
|
if ( infile.samplerate() != gui->samplerate ) {
|
||||||
LUPPP_NOTE("%s%i%s%i", "Resampling from ", infile.samplerate(), " to ", gui->samplerate);
|
LUPPP_NOTE("%s%i%s%i", "Resampling from ", infile.samplerate(), " to ", gui->samplerate);
|
||||||
|
|
||||||
float resampleRatio = float(gui->samplerate) / infile.samplerate();
|
float resampleRatio = float(gui->samplerate) / infile.samplerate();
|
||||||
std::vector<float> resampled( infile.frames() * resampleRatio );
|
std::vector<float> resampledL( infile.frames() / chnls * resampleRatio );
|
||||||
|
std::vector<float> resampledR( infile.frames() / chnls * resampleRatio );
|
||||||
|
|
||||||
SRC_DATA data;
|
SRC_DATA dataL;
|
||||||
data.data_in = &buf[0];
|
SRC_DATA dataR;
|
||||||
data.data_out = &resampled[0];
|
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();
|
dataL.input_frames = infile.frames() / chnls;
|
||||||
data.output_frames = infile.frames() * resampleRatio;
|
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;
|
dataL.end_of_input = 0;
|
||||||
data.src_ratio = resampleRatio;
|
dataR.end_of_input = 0;
|
||||||
|
dataL.src_ratio = resampleRatio;
|
||||||
|
dataR.src_ratio = resampleRatio;
|
||||||
|
|
||||||
int q = SRC_SINC_FASTEST;
|
int q = SRC_SINC_FASTEST;
|
||||||
|
|
||||||
|
@ -223,22 +228,27 @@ int DiskReader::loadSample( int track, int scene, string path )
|
||||||
|
|
||||||
|
|
||||||
// resample quality taken from config file,
|
// resample quality taken from config file,
|
||||||
int ret = src_simple ( &data, q, 1 );
|
int ret = src_simple ( &dataL, q, 1 );
|
||||||
if ( ret == 0 )
|
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
|
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
|
/// exchange buffers, so buf contains the resampled audio
|
||||||
buf.swap( resampled );
|
bufL.swap( resampledL );
|
||||||
|
bufR.swap( resampledR );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// create buffer, and set the data
|
/// create buffer, and set the data
|
||||||
AudioBuffer* ab = new AudioBuffer();
|
AudioBuffer* ab = new AudioBuffer();
|
||||||
ab->setAudioFrames( buf.size() );
|
ab->setAudioFrames( bufL.size() );
|
||||||
// TODO right (stereo)
|
ab->nonRtSetSample( bufL, bufR );
|
||||||
ab->nonRtSetSample( buf, buf );
|
|
||||||
|
|
||||||
//cout << "DiskReader::loadSample() " << path << endl;
|
//cout << "DiskReader::loadSample() " << path << endl;
|
||||||
|
|
||||||
|
|
|
@ -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:
|
// FIXME: the size of the buffer is bigger than the audio contained in it:
|
||||||
// calculate the length that needs saving using getBeats() * framesPerBeat
|
// calculate the length that needs saving using getBeats() * framesPerBeat
|
||||||
if ( ab->getAudioFrames() > 0 )
|
if ( ab->getAudioFrames() > 0 ) {
|
||||||
// TODO right (stereo)
|
for(int i=0; i<ab->getAudioFrames(); i++) {
|
||||||
outfile.write( &ab->getDataL()[0], ab->getAudioFrames() );
|
outfile.writef( &ab->getDataL()[i], 1); //sizeof(ab->getDataL()[i]));
|
||||||
|
outfile.writef( &ab->getDataR()[i], 1); //sizeof(ab->getDataR()[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
LUPPP_WARN("%s","Sample has zero samples");
|
LUPPP_WARN("%s","Sample has zero samples");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue