ArtskydJ / node-sox-stream

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

How to pass additional parameter (ex. the "-s" for "sox tempo -s 0.5") #25

Closed orbitaloop closed 1 year ago

orbitaloop commented 1 year ago

Hi, Thank you for this project, it's super useful. I'm trying to run "sox tempo -s 1.5" with the lib, but I can't manage to pass the "-s" parameter. It's working without it. I know this parameter "-s" is working, because when I run it in the terminal it works: sox normal.mp3 slow.mp3 tempo -s 0.5

I feel I tried everything:

var transform = sox({

            input: { type: 'mp3' },

            output: { type: 'mp3' },

            effects: [//The −s option is used for speech processing.

                'tempo', speedCoeff //working but without the -s, it sounds very robotic

                //'tempo', 's', speedCoeff //not working, crash

                 //'tempo', '-s', speedCoeff //not working crash

                //'tempo -s '+speedCoeff //not working, hang

            ]
    });

Thanks in advance for your help

ArtskydJ commented 1 year ago

I would expect this to work:

var transform = sox({
    input: { type: 'mp3' },
    output: { type: 'mp3' },
    effects: [
        'tempo', '-s', speedCoeff //not working crash
    ]
});

but apparently that's crashing for you.

Maybe you could send the error message you're getting?


Troubleshooting steps:

I recommend editing sox-stream/index.js, and adding this line:

                return flattened.concat(ele)
            }, [])
+       console.log('sox ' + args.join(' '))

        var sox = cp.spawn(opts.soxPath || 'sox', args)
        sox.stdout.pipe(soxOutput)

That will log out the command line arguments being used. Then you can try running it on the command line to try to determine why it crashes. Is it the sox binary that crashes, or javascript?

I'm guessing the output will look like this:

sox --type mp3 normal.mp3 --type mp3 - tempo -s 0.5 > slow.mp3

You can test if sox crashes with that command...

If it does, you might try the following command. If sox does not crash with the following command, but it does crash with the previous command, then sox doesn't like the "output-to-stdout" feature for whatever reason, and you can't use this library.

sox --type mp3 normal.mp3 --type mp3 slow.mp3 tempo -s 0.5
orbitaloop commented 1 year ago

Thank you so much for your help! You were right, I manage to make it work with "'tempo', '-s', speedCoeff". I don't know what happened. I did some refactoring on my code and this morning I tried again and it worked. It was a problem on my side, sorry for the time wasted.

ArtskydJ commented 1 year ago

Glad you got it working!

alechirsch commented 9 months ago

@ArtskydJ I am running into a similar issue with trim. I am running into the issue where it sox-stream hangs, it does not crash. There is no output, and the program never ends. It works when I remove the trim effect, and fails when I add any effect at all. This is the output of the console log, which was successful when I ran it manually and appended to a file

sox --encoding u-law --type raw --rate 8000 --channels 1 /var/folders/lx/pjswfp9j4klf5041516lf70w0000gn/T/20ccd48a-2553-47f4-a2be-3affbe72c6d0 --bits 16 --rate 8000 --channels 1 --type wav - trim 0 5 > test.wav

the second command you suggested also works, it seems to only be a problem when trying with this package.

sox --encoding u-law --type raw --rate 8000 --channels 1 /var/folders/lx/pjswfp9j4klf5041516lf70w0000gn/T/20ccd48a-2553-47f4-a2be-3affbe72c6d0 --bits 16 --rate 8000 --channels 1 --type wav test.wav trim 0 5

Here is my code:

import fs from 'fs';
import sox from 'sox-stream';
function getAudio(start, end) {
    let duration = Math.ceil(end) - Math.floor(start);
    fs.createReadStream('./audio.raw')
        .pipe(sox({
            input: {
                encoding: 'u-law',
                type: 'raw',
                rate: 8000,
                channels: 1
            },
            output: {
                bits: 16,
                rate: 8000,
                channels: 1,
                type: 'wav'
            },
            effects: `trim ${start} ${duration}`
        }))
        .on('error', console.log)
        .pipe(fs.createWriteStream('output.wav'))
        .on('error', console.log);
}
try {
    getAudio(0, 5);
}
catch (error) {
    console.log(error);
}

this might be related to #19

alechirsch commented 9 months ago

I tested a couple things with child_process. I changed the spawn to

var sox = cp.spawn(opts.soxPath || 'sox', args, { stdio: 'inherit' })

and got this output when running the script

sox FAIL formats: can't open input  `-': WAVE: RIFF header not found