-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.in
More file actions
112 lines (81 loc) · 2.63 KB
/
README.in
File metadata and controls
112 lines (81 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
## 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](https://www.osgi.org/)
* [JPMS-ready](https://en.wikipedia.org/wiki/Java_Platform_Module_System)
* 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
);
```