Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public class SampleBufferFromToShort extends SampleBuffer {
  2. private final double floatOffset, floatScale;
  3.  
  4. public SampleBufferFromToShort(NoiseCanceller nc, int validBits) {
  5. super(nc);
  6.  
  7. if (validBits > 8) {
  8. // If more than 8 validBits, data is signed
  9. // Conversion required multiplying by magnitude of max positive value
  10. floatOffset = 0;
  11. floatScale = Long.MAX_VALUE >> (64 - validBits);
  12. } else {
  13. // Else if 8 or less validBits, data is unsigned
  14. // Conversion required dividing by max positive value
  15. floatOffset = 1;
  16. floatScale = 0.5 * ((1 << validBits) - 1);
  17. }
  18. }
  19.  
  20. public void process(short[] sample, int pos) {
  21. // where to copy our current sample to
  22. double[] buffer = getCurrentInputSampleBuffer();
  23.  
  24. // copy sample to current sample buffer
  25. convertFromShort(sample, pos, buffer);
  26.  
  27. // process sample and return product (delay of 1 sample)
  28. double[] product = process();
  29.  
  30. // overwrite original sample with product
  31. convertToShort(sample, pos, product);
  32. }
  33.  
  34. private void convertFromShort(short[] sample, int pos, double[] buffer) {
  35. for (int f = pos; f < buffer.length; f++) {
  36. buffer[f-pos] = floatOffset + (double) sample[pos] / floatScale;
  37. }
  38. }
  39.  
  40. private void convertToShort(short[] sample, int pos, double[] buffer) {
  41. for (int f = pos; f < buffer.length; f++) {
  42. sample[pos] = (short)((buffer[f-pos] - floatOffset) * floatScale);
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement