Watch
2
0
Fork
You've already forked jsamplebuffer
0
Audio sample buffer types
  • Java 91.2%
  • Shell 8.8%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Mark Raynsford 3360499bd4
Some checks are pending
main-RHEL10-x86_64-jdk25 / Build (push) Waiting to run
main-RHEL10-x86_64-jdk26 / Build (push) Waiting to run
main-RHEL9-x86_64-jdk25 / Build (push) Waiting to run
main-RHEL9-x86_64-jdk26 / Build (push) Waiting to run
Update workflows.
2026-07-31 18:42:56 +01:00
.forgejo Update workflows. 2026-07-31 18:42:56 +01:00
com.io7m.jsamplebuffer.api Begin next development iteration. 2025-12-22 12:22:12 +00:00
com.io7m.jsamplebuffer.tests Begin next development iteration. 2025-12-22 12:22:12 +00:00
com.io7m.jsamplebuffer.vanilla Begin next development iteration. 2025-12-22 12:22:12 +00:00
com.io7m.jsamplebuffer.xmedia Begin next development iteration. 2025-12-22 12:22:12 +00:00
src/site/resources Update site metadata. 2025-08-09 15:07:02 +00:00
.gitignore Update .gitignore. 2024-05-07 16:01:44 +00:00
.gitmodules Move to new organization. 2024-04-30 21:19:56 +00:00
doc7m-books.json Add doc7m-books.json 2026-07-31 18:36:47 +01:00
pom.xml Migrate project. 2026-06-27 10:11:07 +01:00
README-CHANGES.xml Mark release 1.0.1 2025-12-22 12:21:56 +00:00
README-LICENSE.txt Update LICENSE 2023-08-13 21:16:10 +00:00
README.in Migrate project. 2026-06-27 10:11:07 +01:00
README.md Migrate project. 2026-06-27 10:11:07 +01:00

jsamplebuffer

Maven Central Maven Central (snapshot) Java Version

com.io7m.jsamplebuffer

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
);