ArtskydJ / node-sox-stream

:mega: A stream-friendly wrapper around SoX
53 stars 14 forks source link

Get it works with bpm.js #8

Closed roccomuso closed 5 years ago

roccomuso commented 6 years ago

Can you take a look at this? https://github.com/monstercat/bpm.js/issues/5

I'm trying to make this module works with bpm.js.

I'm trying to do with sox-stream the equivalent of this:

var spawn = require('child_process').spawn

function createAudioStream(filename) {
  var args = "-t raw -r 44100 -e float -c 1 -".split(" ")
  args.unshift(filename)
  var sox = spawn("sox", args)
  return sox.stdout
}
ArtskydJ commented 6 years ago

@roccomuso does bpm.js work with your createAudioStream() function?

Please post a stack trace of the error you're getting.

roccomuso commented 6 years ago

@ArtskydJ it works with the createAudioStream() function.

The code I'm trying to execute is this:

var sox = require('sox-stream')
var fs  = require('fs')
var src = fs.createReadStream('hellberg.mp3')

var transcode = sox({
        input: {
                type: 'mp3',
                encoding: 'float',
                rate: 44100,
                channels: 1
        },
        output: {
                type: 'mp3'
        }
})

src.pipe(transcode)

and returns this error:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: sox WARN formats: can't set 1 channels; using 2

    at Socket.<anonymous> (/tmp/hello/node_modules/sox-stream/index.js:33:23)

Changing channel to 2 apparently i got a valid stream. But when piped to bpm.js after a while you get this:

/tmp/bpm.js/index.js:0

RangeError: Index out of range
    at checkOffset (buffer.js:830:11)
    at Buffer.readFloatLE (buffer.js:1007:5)
    at bufRead (/tmp/bpm.js/index.js:129:14)
    at DestroyableTransform.write [as _transform] (/tmp/bpm.js/index.js:148:15)
    at DestroyableTransform.Transform._read (/tmp/bpm.js/node_modules/readable-stream/lib/_stream_transform.js:182:10)
    at DestroyableTransform.Transform._write (/tmp/bpm.js/node_modules/readable-stream/lib/_stream_transform.js:170:83)
    at doWrite (/tmp/bpm.js/node_modules/readable-stream/lib/_stream_writable.js:406:64)
    at writeOrBuffer (/tmp/bpm.js/node_modules/readable-stream/lib/_stream_writable.js:395:5)
    at DestroyableTransform.Writable.write (/tmp/bpm.js/node_modules/readable-stream/lib/_stream_writable.js:322:11)
    at Stream.ondata (stream.js:31:26)
ArtskydJ commented 6 years ago

If you use sox-stream with 2 channels, and pipe it to fs.createWriteStream('mynewfile.mp3'), and then run your bpm code with fs.createReadStream('mynewfile.mp3'), then does bpm.js accept it?

My guess is that sox doesn't like mono audio, and bpm.js doesn't like stereo?

ArtskydJ commented 6 years ago

@roccomuso since I haven't heard from you, I'm gonna close this issue. @ reply if you have any questions.

roccomuso commented 6 years ago

Sorry, 've been busy lately.

I did what you said. bpm.js keeps saying "Range Error: Index out of range".

NB. mynewfile.mp3 is 725159 bytes. The original mp3 is 725240. We have 81 bytes of difference.

roccomuso commented 6 years ago

With the original sox from CLI (using this cmd: sox wave.mp3 -t raw -r 44100 -e float -c 2 out.mp3), the output file size is: 15980544.

With the cli tool I'm also able to set -c 1, with sox-stream it throws: Error: sox WARN formats: can't set 1 channels; using 2

ArtskydJ commented 6 years ago

I looked at bpm.js code, and this line return arr.readFloatLE(i*4); appears to be erroring.

Maybe run the mp3 through sox-stream, and then write it to the file system. Then read it from the file system and pipe it through bpm.js? If that works, then it is an issue with the interface of sox-stream and bpm.js aren't compatible. (Which would be weird since they're both streams.) But if that's the case, you might just want to use sox.js instead of sox-stream.

roccomuso commented 6 years ago

It's exactly what i did Range Error: Index out of range. It's some other issue.

ArtskydJ commented 6 years ago

So if you run the file through sox using the command line using the same options you used with sox-stream, and then you read the file using bpm.js, are you getting the Range Error: Index out of range error then too?

Maybe try an assortment of mp3s?

roccomuso commented 6 years ago

yes. I should try with different mp3..

ArtskydJ commented 5 years ago

Closing since this has been inactive for over half of a year. Reply or re-open if this is still an issue.