TooTallNate / node-lame

Node.js native bindings to libmp3lame & libmpg123
MIT License
567 stars 113 forks source link

How to change play speed? #90

Open stheine opened 5 years ago

stheine commented 5 years ago

I am playing an audio PCM stream in javascript and would like to adjust the playback speed? how can I do this?

my relevant piece of code is:

const lame    = require('lame');
const request = require('request');
const Speaker = require('speaker');

const decoder = new lame.Decoder();
const speaker = new Speaker({
  channels:   2,
  bitDepth:   16,
  sampleRate: 44100,
  mode:       lame.STEREO,
  device:     'hw:1,0',
});

const req = request.get(url);

req
  .pipe(decoder)
  .pipe(speaker);

So, for example, can I somehow speed up the playback to 10% or 20%?

LinusU commented 5 years ago

I'm afraid that this module won't help you with this, you'll have to insert a Transform stream between lame and speaker which does this at the raw audio bytes level:

 const lame    = require('lame')
 const request = require('request')
 const Speaker = require('speaker')
+const TempoModifier = require(...)

 const decoder = new lame.Decoder()
+const tempo = new TempoModifier(1.10)
 const speaker = new Speaker({
   channels:   2,
   bitDepth:   16,
   sampleRate: 44100,
   mode:       lame.STEREO,
   device:     'hw:1,0',
 })

 const req = request.get(url)

 req
   .pipe(decoder)
+  .pipe(tempo)
   .pipe(speaker)
stheine commented 5 years ago

thanks for that info.

are you aware of any module that can be used as TempoModifier. or is that code that I/someone needs to write from scratch?

LinusU commented 5 years ago

Not aware of any such module, but could potentially exist somewhere on npm ☺️