jsamplebuffer
The jsamplebuffer
package implements a set of types and
functions for manipulating buffers of audio data.
Features
- Functions for safely reading and writing sample data.
- Convenience functions for constructing sample buffers from
javax.sound
streams. - OSGi-ready
- JPMS-ready
- ISC license.
- High-coverage automated test suite.
Usage
Creating Buffers
Use SampleBufferDouble
and SampleBufferFloat
to create buffers of audio
where samples are stored as double
or float
values, respectively:
int channels = 2;
long frames = 100L;
double sampleRate = 44100.0;
final var buffer =
SampleBufferDouble.createWithHeapBuffer(
channels,
frames,
sampleRate
);
assert 2 == buffer.channels();
assert 100L == buffer.frames();
assert 200L == buffer.samples();
Additional methods are provided for creating buffers from ByteBuffer
values,
and from direct memory.
Reading Buffers
Use the frameGetExact
method to read a single frame from a buffer:
double samples[] = new double[2];
buffer.frameGetExact(0L, samples);
// samples[0] now contains the sample value for the first channel
// samples[1] now contains the sample value for the second channel
Writing Buffers
Use the frameSetExact
method to write a single frame to a buffer:
double samples[] = new double[2];
samples[0] = 0.5;
samples[1] = 0.75;
buffer.frameSetExact(0L, samples);
Additional methods are provided that are specialized to mono and stereo buffers. Additional methods are provided that allow for filling all channels of a frame with a single sample value.
Converting Buffers
Implementations of the SampleBufferRateConverterType
can be used to
convert buffers between different sampling rates. Currently, one
implementation is provided, SXMSampleBufferRateConverters
, that uses the
JVM's javax.sound
conversion machinery. The following code converts a
given buffer to a sample rate of 22050hz
:
SampleBufferType srcBuffer;
SXMSampleBufferRateConverters converters;
converters.createConverter()
.convert(
SampleBufferDouble::createWithHeapBuffer,
srcBuffer,
22050.0
);
Parsing And Serializing Buffers
The SXMSampleBuffers
class contains numerous convenience methods for
reading/writing audio buffers from/to audio files using javax.sound
.
Path inputFile;
Path outputFile;
final var buffer =
SXMSampleBuffers.readSampleBufferFromFile(
inputFile,
SampleBufferDouble::createWithHeapBuffer
);
SXMSampleBuffers.writeSampleBufferToFile(
buffer,
outputFile
);
Releases & Development Snapshots
Releases
You can subscribe to the atom feed to be notified of project releases.
The most recently released version of the package is 1.0.0.
1.0.0 Release (2024-05-16Z)
- Initial public release.
The compiled artifacts for the release (and all previous releases) are available on Maven Central.
Maven Modules
<dependency> <group>com.io7m.jsamplebuffer</group> <artifactId>com.io7m.jsamplebuffer.api</artifactId> <version>1.0.0</version> </dependency><dependency> <group>com.io7m.jsamplebuffer</group> <artifactId>com.io7m.jsamplebuffer.vanilla</artifactId> <version>1.0.0</version> </dependency><dependency> <group>com.io7m.jsamplebuffer</group> <artifactId>com.io7m.jsamplebuffer.tests</artifactId> <version>1.0.0</version> </dependency><dependency> <group>com.io7m.jsamplebuffer</group> <artifactId>com.io7m.jsamplebuffer.xmedia</artifactId> <version>1.0.0</version> </dependency>
Development Snapshots
At the time of writing, the current unstable development version of the package is 1.0.1-SNAPSHOT.
Development snapshots may be available in the Central Portal Snapshots repository. Snapshots are published to this repository every time the project is built by the project's continuous integration system, but snapshots do expire after around ninety days and so may or may not be available depending on when a build of the package was last triggered.
Manual
This project does not have any user manuals or other documentation beyond what might be present on the page above.
Sources
This project uses Git to manage source code.
Repository: https://www.github.com/io7m-com/jsamplebuffer
$ git clone --recursive https://www.github.com/io7m-com/jsamplebuffer
Issues
This project uses GitHub Issues to track issues.
License
Copyright © 2023 Mark Raynsford <code@io7m.com> https://www.io7m.com Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.