Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SampleBufferFromToShort extends SampleBuffer {
- private final double floatOffset, floatScale;
- public SampleBufferFromToShort(NoiseCanceller nc, int validBits) {
- super(nc);
- if (validBits > 8) {
- // If more than 8 validBits, data is signed
- // Conversion required multiplying by magnitude of max positive value
- floatOffset = 0;
- floatScale = Long.MAX_VALUE >> (64 - validBits);
- } else {
- // Else if 8 or less validBits, data is unsigned
- // Conversion required dividing by max positive value
- floatOffset = 1;
- floatScale = 0.5 * ((1 << validBits) - 1);
- }
- }
- public void process(short[] sample, int pos) {
- // where to copy our current sample to
- double[] buffer = getCurrentInputSampleBuffer();
- // copy sample to current sample buffer
- convertFromShort(sample, pos, buffer);
- // process sample and return product (delay of 1 sample)
- double[] product = process();
- // overwrite original sample with product
- convertToShort(sample, pos, product);
- }
- private void convertFromShort(short[] sample, int pos, double[] buffer) {
- for (int f = pos; f < buffer.length; f++) {
- buffer[f-pos] = floatOffset + (double) sample[pos] / floatScale;
- }
- }
- private void convertToShort(short[] sample, int pos, double[] buffer) {
- for (int f = pos; f < buffer.length; f++) {
- sample[pos] = (short)((buffer[f-pos] - floatOffset) * floatScale);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement