Skip to content

Commit

Permalink
more accurate loading of mp3 files
Browse files Browse the repository at this point in the history
  • Loading branch information
ddf committed Jan 3, 2015
1 parent 428f834 commit b8afdc1
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 26 deletions.
12 changes: 1 addition & 11 deletions examples/Advanced/loadFileIntoBuffer/loadFileIntoBuffer.pde
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void setup()

// create Minim and an AudioOutput
minim = new Minim(this);
minim.debugOn();
output = minim.getLineOut();

// construct a new MultiChannelBuffer with 2 channels and 1024 sample frames.
Expand All @@ -36,16 +35,7 @@ void setup()
// we pass the buffer to the method and Minim will reconfigure it to match
// the file. if the file doesn't exist, or there is some other problen with
// loading it, the function will return 0 as the sample rate.
float sampleRate = 0;
try
{
sampleRate = minim.loadFileIntoBuffer( "SD.mp3", sampleBuffer );
}
catch( Exception ex )
{
println( ex.toString() );
ex.printStackTrace();
}
float sampleRate = minim.loadFileIntoBuffer( "SD.wav", sampleBuffer );

// make sure the file load worked
if ( sampleRate > 0 )
Expand Down
Binary file modified library/jsminim.jar
Binary file not shown.
Binary file modified library/minim.jar
Binary file not shown.
19 changes: 16 additions & 3 deletions src/ddf/minim/Minim.java
Original file line number Diff line number Diff line change
Expand Up @@ -646,21 +646,34 @@ public float loadFileIntoBuffer( String filename, MultiChannelBuffer outBuffer )
readBuffer.setBufferSize( (int)(totalSampleCount - totalSamplesRead) );
}

stream.read( readBuffer );
int samplesRead = stream.read( readBuffer );

if ( samplesRead == 0 )
{
debug( "loadSampleIntoBuffer: got 0 samples read" );
break;
}

// copy data from one buffer to the other.
for(int i = 0; i < channelCount; ++i)
{
// a faster way to do this would be nice.
for(int s = 0; s < readBuffer.getBufferSize(); ++s)
for(int s = 0; s < samplesRead; ++s)
{
outBuffer.setSample( i, (int)totalSamplesRead+s, readBuffer.getSample( i, s ) );
}
}

totalSamplesRead += readBuffer.getBufferSize();
totalSamplesRead += samplesRead;
}

if ( totalSamplesRead != totalSampleCount )
{
outBuffer.setBufferSize( (int)totalSamplesRead );
}

debug("loadSampleIntoBuffer: final output buffer size is " + outBuffer.getBufferSize() );

stream.close();
}
else
Expand Down
4 changes: 3 additions & 1 deletion src/ddf/minim/javasound/JSAudioInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public float[] read()
return samples;
}

public void read(MultiChannelBuffer buffer)
public int read(MultiChannelBuffer buffer)
{
// create our converter object
int numChannels = line.getFormat().getChannels();
Expand All @@ -176,5 +176,7 @@ public void read(MultiChannelBuffer buffer)
{
buffer.setChannel(i, convert.getChannel(i));
}

return numSamples;
}
}
4 changes: 2 additions & 2 deletions src/ddf/minim/javasound/JSAudioRecording.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ public float[] read()
return null;
}

public void read(MultiChannelBuffer buffer)
public int read(MultiChannelBuffer buffer)
{

return 0;
}
}
4 changes: 2 additions & 2 deletions src/ddf/minim/javasound/JSAudioRecordingClip.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public float[] read()
return null;
}

public void read(MultiChannelBuffer buffer)
public int read(MultiChannelBuffer buffer)
{

return 0;
}
}
21 changes: 15 additions & 6 deletions src/ddf/minim/javasound/JSBaseAudioRecordingStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void sleep(int millis)
}
}

private void readBytes()
private int readBytes()
{
int bytesRead = 0;
int toRead = rawBytes.length;
Expand Down Expand Up @@ -236,6 +236,8 @@ private void readBytes()
system.error( "Error reading from the file - " + e.getMessage() );
}
totalBytesRead += bytesRead;

return bytesRead;
}

private void readBytesLoop()
Expand Down Expand Up @@ -663,16 +665,17 @@ public float[] read()
}

// FIXME: temporary implementation of read
public void read(MultiChannelBuffer outBuffer)
public int read(MultiChannelBuffer outBuffer)
{
if ( buffer.getSampleCount() != outBuffer.getBufferSize() )
{
buffer.changeSampleCount( outBuffer.getBufferSize(), true );
rawBytes = new byte[buffer.getByteArrayBufferSize( format )];
}
int framesRead = 0;
if ( play )
{
mRead();
framesRead = mRead();
}
else
{
Expand All @@ -682,24 +685,30 @@ public void read(MultiChannelBuffer outBuffer)
{
outBuffer.setChannel( i, buffer.getChannel(i) );
}

return framesRead;
}

private void mRead()
// returns number of samples read, not bytes
private int mRead()
{
// read in a full buffer of bytes from the file
int bytesRead = rawBytes.length;
if ( loop )
{
readBytesLoop();
}
else
{
readBytes();
bytesRead = readBytes();
}
// convert them to floating point
int frameCount = bytesRead / format.getFrameSize();
synchronized ( buffer )
{
int frameCount = rawBytes.length / format.getFrameSize();
buffer.setSamplesFromBytes( rawBytes, 0, format, 0, frameCount );
}

return frameCount;
}
}
4 changes: 3 additions & 1 deletion src/ddf/minim/spi/AudioStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public interface AudioStream extends AudioResource
* AudioStream does.
*
* @param buffer The MultiChannelBuffer to fill with audio samples.
*
* @return int: the number of sample frames that were actually read, could be smaller than the size of the buffer.
*/
void read(MultiChannelBuffer buffer);
int read(MultiChannelBuffer buffer);
}

0 comments on commit b8afdc1

Please sign in to comment.