Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. // Based on discord.js' old volume system
  2.  
  3. const { Transform } = require('stream');
  4. const loader = require('../util/loader');
  5.  
  6. const krypton = loader.require([['krypton']]).fn;
  7. class VolumeTransformer extends Transform {
  8. constructor(options = {}) {
  9. super(options);
  10. this._bits = options.bits;
  11. this._bytes = this._bits / 8;
  12. this._extremum = Math.pow(2, this._bits - 1) - 1;
  13. this.volume = options.volume || 1;
  14. this._chunk = Buffer.alloc(0);
  15. }
  16.  
  17. _readInt(buffer, index) { return index; }
  18. _writeInt(buffer, int, index) { return index; }
  19.  
  20. _transform(chunk, encoding, done) {
  21. // If the volume is 1, act like a passthrough stream
  22. if (this.volume === 1) {
  23. this.push(chunk);
  24. return done();
  25. }
  26.  
  27. const { _bytes, _extremum } = this;
  28.  
  29. chunk = this._chunk = Buffer.concat([this._chunk, chunk]);
  30. if (chunk.length < _bytes) return done();
  31.  
  32. let transformed;
  33. let complete;
  34.  
  35. if (krypton && krypton.pcm.simd && this._krypton) {
  36. if (chunk.length < 64) return done();
  37. transformed = this._krypton(chunk.slice(0, chunk.length - (chunk.length % 64)), this.volume);
  38. complete = transformed.length;
  39. } else {
  40. transformed = Buffer.alloc(chunk.length);
  41. complete = Math.floor(chunk.length / _bytes) * _bytes;
  42.  
  43. for (let i = 0; i < complete; i += _bytes) {
  44. const int = Math.min(_extremum, Math.max(-_extremum, Math.floor(this.volume * this._readInt(chunk, i))));
  45. this._writeInt(transformed, int, i);
  46. }
  47. }
  48.  
  49. this._chunk = chunk.slice(complete);
  50. this.push(transformed);
  51. return done();
  52. }
  53.  
  54. _destroy(err, cb) {
  55. super._destroy(err, cb);
  56. this._chunk = null;
  57. }
  58.  
  59. setVolume(volume) {
  60. this.volume = volume;
  61. }
  62.  
  63. setVolumeDecibels(db) {
  64. this.setVolume(Math.pow(10, db / 20));
  65. }
  66.  
  67. setVolumeLogarithmic(value) {
  68. this.setVolume(Math.pow(value, 1.660964));
  69. }
  70.  
  71. get volumeDecibels() {
  72. return Math.log10(this._volume) * 20;
  73. }
  74.  
  75. get volumeLogarithmic() {
  76. return Math.pow(this._volume, 1 / 1.660964);
  77. }
  78. }
  79.  
  80. class VolumeTransformer16LE extends VolumeTransformer {
  81. constructor(...options) { super({ options, bits: 16 }); }
  82. _readInt(buffer, index) { return buffer.readInt16LE(index); }
  83. _writeInt(buffer, int, index) { return buffer.writeInt16LE(int, index); }
  84.  
  85. _krypton(buffer, volume) {
  86. return krypton.do(krypton.pcm.volume16(buffer, volume)).run(false);
  87. }
  88. }
  89.  
  90. class VolumeTransformer16BE extends VolumeTransformer {
  91. constructor(...options) { super({ options, bits: 16 }); }
  92. _readInt(buffer, index) { return buffer.readInt16BE(index); }
  93. _writeInt(buffer, int, index) { return buffer.writeInt16BE(int, index); }
  94. }
  95.  
  96. class VolumeTransformer32LE extends VolumeTransformer {
  97. constructor(...options) { super({ options, bits: 32 }); }
  98. _readInt(buffer, index) { return buffer.readInt32LE(index); }
  99. _writeInt(buffer, int, index) { return buffer.writeInt32LE(int, index); }
  100. }
  101.  
  102. class VolumeTransformer32BE extends VolumeTransformer {
  103. constructor(...options) { super({ options, bits: 32 }); }
  104. _readInt(buffer, index) { return buffer.readInt32BE(index); }
  105. _writeInt(buffer, int, index) { return buffer.writeInt32BE(int, index); }
  106. }
  107.  
  108. module.exports = {
  109. VolumeTransformer16LE,
  110. VolumeTransformer16BE,
  111. VolumeTransformer32LE,
  112. VolumeTransformer32BE,
  113. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement