Skip to content

Commit

Permalink
Fix reading and writing of stereo files
Browse files Browse the repository at this point in the history
  • Loading branch information
coderkun authored and harryhaaren committed Mar 27, 2017
1 parent 6a64312 commit ed72627
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
27 changes: 17 additions & 10 deletions src/diskreader.cxx
Expand Up @@ -165,9 +165,9 @@ int DiskReader::loadSample( int track, int scene, string path )
/// load the sample
SndfileHandle infile( path, SFM_READ );
int chnls = infile.channels();
std::vector<float> bufL( infile.frames() / chnls );
std::vector<float> bufR( infile.frames() / chnls );
std::vector<float> bufTmp( infile.frames() * (chnls-2) );
std::vector<float> bufL( infile.frames() );
std::vector<float> bufR( infile.frames() );
float frameBuf[ sizeof(float) * chnls ];

if ( infile.error() ) {
LUPPP_ERROR("File %s, Error %s", path.c_str(), infile.strError() );
Expand All @@ -177,13 +177,20 @@ int DiskReader::loadSample( int track, int scene, string path )
LUPPP_NOTE("Loading file with %i chnls, frames %i, bufferL size %i bufferR size %i", infile.channels(), infile.frames(), bufL.size(), bufR.size() );

// Read data
for(int f=0; f<infile.frames()/chnls; f++) {
infile.read( (float*)&bufL[f] , 1 );
infile.read( (float*)&bufR[f] , 1 );

// Read (and ignore) remaining channels
for(int c=0; c<chnls-2; c++) {
infile.read( (float*)&bufTmp[f], 1);
for(int f=0; f<infile.frames(); f++) {
infile.readf( frameBuf, 1 );

// Frist sapmle
// used for both channels when file is mono
// used for left channel when file is stereo
if(chnls > 0) {
bufL[f] = frameBuf[0];
bufR[f] = frameBuf[0];
}
// Second sample
// used for right channel when file is stereo
if(chnls > 1) {
bufR[f] = frameBuf[1];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/diskwriter.cxx
Expand Up @@ -321,9 +321,11 @@ int DiskWriter::writeAudioBuffer(int track, int scene, AudioBuffer* ab,
// 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 ) {
float frameBuf[ sizeof(float) * 2 ];
for(int i=0; i<ab->getAudioFrames(); i++) {
outfile.writef( &ab->getDataL()[i], 1); //sizeof(ab->getDataL()[i]));
outfile.writef( &ab->getDataR()[i], 1); //sizeof(ab->getDataR()[i]));
frameBuf[0] = ab->getDataL()[i];
frameBuf[1] = ab->getDataR()[i];
outfile.writef( frameBuf, 1);
}
}
else {
Expand Down

0 comments on commit ed72627

Please sign in to comment.