summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/SampleBuffer.java
blob: af618c6960c8182c138ae1ff68b8a1dcda88047d (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (c) 2017 - 2019, SmartDeviceLink Consortium, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the SmartDeviceLink Consortium, Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this 
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.smartdevicelink.managers.audio;

import android.util.Log;

import com.smartdevicelink.managers.audio.AudioStreamManager.SampleType;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Wraps a buffer of raw audio samples depending on the sample type (8 bit, 16 bit)
 * Unifies samples into double.
 */
public class SampleBuffer {
    private static final String TAG = SampleBuffer.class.getSimpleName();

    @SuppressWarnings({"unused", "FieldCanBeLocal"})
    private @SampleType final int sampleType;
    private final ByteBuffer byteBuffer;
    private final int channelCount;
    private final long presentationTimeUs;

    /**
     * Wraps a raw (mono) byte buffer to a new sample buffer.
     * @param buffer The raw buffer to be wrapped.
     * @param sampleType The sample type of the samples in the raw buffer.
     * @param presentationTimeUs The presentation time of the buffer.
     * @return A new sample buffer wrapping the specified raw buffer.
     */
    public static SampleBuffer wrap(ByteBuffer buffer, @SampleType int sampleType, long presentationTimeUs) {
        return new SampleBuffer(buffer, sampleType, 1, presentationTimeUs);
    }

    /**
     * Wraps a raw byte buffer to a new sample buffer.
     * @param buffer The raw buffer to be wrapped.
     * @param sampleType The sample type of the samples in the raw buffer.
     * @param channelCount The number of channels (1 = mono, 2 = stereo).
     * @param presentationTimeUs The presentation time of the buffer.
     * @return A new sample buffer wrapping the specified raw buffer.
     */
    public static SampleBuffer wrap(ByteBuffer buffer, @SampleType int sampleType, int channelCount, long presentationTimeUs) {
        return new SampleBuffer(buffer, sampleType, channelCount, presentationTimeUs);
    }

    /**
     * Allocates a new sample buffer.
     * @param capacity The specified sample capacity of the sample buffer.
     * @param sampleType The sample type of the samples the buffer should store.
     * @param byteOrder The byte order for the samples (little or big endian).
     * @param presentationTimeUs The presentation time for the buffer.
     * @return A new and empty sample buffer.
     */
    public static SampleBuffer allocate(int capacity, @SampleType int sampleType, ByteOrder byteOrder, long presentationTimeUs) {
        return new SampleBuffer(capacity, sampleType, 1, byteOrder, presentationTimeUs);
    }

    /**
     * Allocates a new sample buffer.
     * @param capacity The specified sample capacity of the sample buffer.
     * @param sampleType The sample type of the samples the buffer should store.
     * @param channelCount The number of channels (1 = mono, 2 = stereo).
     * @param byteOrder The byte order for the samples (little or big endian).
     * @param presentationTimeUs The presentation time for the buffer.
     * @return A new and empty sample buffer.
     */
    @SuppressWarnings("unused")
    public static SampleBuffer allocate(int capacity, @SampleType int sampleType, int channelCount, ByteOrder byteOrder, long presentationTimeUs) {
        return new SampleBuffer(capacity, sampleType, channelCount, byteOrder, presentationTimeUs);
    }

    private SampleBuffer(int capacity, @SampleType int sampleType, int channelCount, ByteOrder byteOrder, long presentationTimeUs) {
        this.byteBuffer = ByteBuffer.allocate(sampleType * capacity);
        this.byteBuffer.order(byteOrder);
        this.sampleType = sampleType;
        this.channelCount = channelCount;
        this.presentationTimeUs = presentationTimeUs;
    }

    private SampleBuffer(ByteBuffer buffer, @SampleType int sampleType, int channelCount, long presentationTimeUs) {
        this.byteBuffer = buffer;
        this.sampleType = sampleType;
        this.channelCount = channelCount;
        this.presentationTimeUs = presentationTimeUs;
    }

    /**
     * Returns the capacity of the buffer per channel.
     */
    @SuppressWarnings("unused")
    public int capacity() {
        return byteBuffer.capacity() / sampleType / channelCount;
    }

    /**
     * Returns the number of samples in the buffer per channel.
     */
    public int limit() {
        return byteBuffer.limit() / sampleType / channelCount;
    }

    /**
     * Sets the number of samples in the buffer to the new limit.
     * @param newLimit The new limit of the sample buffer.
     */
    public void limit(int newLimit) {
        byteBuffer.limit(newLimit * sampleType * channelCount);
    }

    /**
     * Returns the current position in the buffer per channel.
     * @return The position of the sample buffer.
     */
    public int position() {
        return byteBuffer.position() / sampleType / channelCount;
    }

    /**
     *Sets the position of the sample buffer to the new index.
     * @param newPosition The new position of the sample buffer.
     */
    public void position(int newPosition) {
        byteBuffer.position(newPosition * sampleType * channelCount);
    }

    /**
     * Returns the sample of the current position and then increments the position.
     * The sample returned is a mixed sample getting all samples from each channel.
     * @return The mixed sample.
     */
    public double get() {
        // convenient method to avoid duplicate code: we use -1 index to call get()
        return get(-1);
    }

    /**
     * Returns the sample from the given index in the buffer.
     * If the buffer's channel count is > 1 the sample returned
     * is a mixed sample getting all samples from each channel.
     * @param index The index of the sample requested.
     * @return The sample requested.
     */
    public double get(int index) {
        int internalIndex = index * channelCount * sampleType;

        switch (sampleType) {
            case SampleType.UNSIGNED_8_BIT: {
                double avg = 0;

                // get a sample mix to mono from the index
                for (int i = 0; i < channelCount; i++) {
                    byte b = index == -1 ? byteBuffer.get() : byteBuffer.get(internalIndex + i * sampleType);
                    int a = b & 0xff; // convert the 8 bits into int so we can calc > 127
                    avg += a / (double)channelCount;
                }

                return avg * 2.0 / 255.0 - 1.0; //magic? check out SampleType
            }
            case SampleType.SIGNED_16_BIT: {
                double avg = 0;

                // get a sample mix to mono from the index
                for (int i = 0; i < channelCount; i++) {
                    short a = index == -1 ? byteBuffer.getShort() : byteBuffer.getShort(internalIndex + i * sampleType);
                    avg += a / (double)channelCount;
                }

                return (avg + 32768.0) * 2.0 / 65535.0 - 1.0; //magic? check out SampleType
            }
            case SampleType.FLOAT: {
                double avg = 0;

                // get a sample mix to mono from the index
                for (int i = 0; i < channelCount; i++) {
                    double a = index == -1 ? byteBuffer.getFloat() : byteBuffer.getFloat(internalIndex + i * sampleType);
                    avg += a / (double)channelCount;
                }

                return avg;
            }
            default: {
                Log.e(TAG, "SampleBuffer.get(int): The sample type is not known: " + sampleType);
                return 0.0;
            }
        }
    }

    /**
     * Puts a sample to the current position and increments the position.
     * @param sample The sample to put into the buffer.
     */
    public void put(double sample) {
        put(-1, sample);
    }

    /**
     * Puts a sample to the given index in the buffer.
     * If the buffer's channel count is > 1 the sample
     * will be stored in each channel at the given index.
     * @param index The index to put the sample.
     * @param sample The sample to store in the buffer.
     */
    public void put(int index, double sample) {
        int internalIndex = index * channelCount * sampleType;
        switch (sampleType) {
            case SampleType.UNSIGNED_8_BIT: {
                int a = (int)Math.round((sample + 1.0) * 255.0 / 2.0); //magic? check out SampleType
                byte b = (byte)a;
                if (index == -1) {
                    for (int i = 0; i < channelCount; i++) {
                        byteBuffer.put(b);
                    }
                } else {
                    for (int i = 0; i < channelCount; i++) {
                        byteBuffer.put(internalIndex + i * sampleType, b);
                    }
                }
                break;
            }
            case SampleType.SIGNED_16_BIT: {
                short a = (short)Math.round((sample + 1.0) * 65535 / 2.0 - 32767.0); //magic? check out SampleType
                if (index == -1) {
                    for (int i = 0; i < channelCount; i++) {
                        byteBuffer.putShort(a);
                    }
                } else {
                    for (int i = 0; i < channelCount; i++) {
                        byteBuffer.putShort(internalIndex + i * sampleType, a);
                    }
                }
                break;
            }
            case SampleType.FLOAT: {
                if (index == -1) {
                    for (int i = 0; i < channelCount; i++) {
                        byteBuffer.putFloat((float) sample);
                    }
                } else {
                    for (int i = 0; i < channelCount; i++) {
                        byteBuffer.putFloat(internalIndex + i * sampleType, (float) sample);
                    }
                }
                break;
            }
            default: {
                Log.e(TAG, "SampleBuffer.set(int): The sample type is not known: " + sampleType);
            }
        }
    }

    /**
     * Returns the raw byte buffer managed by this sample buffer.
     * @return The raw byte buffer managed by this sample buffer.
     */
    public ByteBuffer getByteBuffer() {
        return byteBuffer;
    }

    /**
     * Returns a copy of the bytes from position 0 to the current limit.
     * @return A copy of the bytes.
     */
    public byte[] getBytes() {
        int limit = byteBuffer.limit();
        byte[] bytes = new byte[limit];

        for (int i = 0; i < limit; ++i) {
            bytes[i] = byteBuffer.get(i);
        }

        return bytes;
    }

    /**
     * The presentation time of this sample buffer.
     * @return The presentation time of this sample buffer.
     */
    public long getPresentationTimeUs() {
        return presentationTimeUs;
    }
}