Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ sox({
console.log(err) // => null
console.log(outputFilePath) // => song2.flac
})


or

sox({
input: { volume: 0.8 },
inputFile: 'song.flac',
outputFile: 'song2.flac'
})
.then(outputFilePath => {
console.log(outputFilePath) // => song2.flac
})
.catch(err => console.log(err))


```

Transcode with options and effects:
Expand Down
61 changes: 32 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
var spawn = require('child_process').spawn
var hashToArray = require('hash-to-array')
var onetime = require('onetime')
const spawn = require('child_process').spawn
const hashToArray = require('hash-to-array')
const onetime = require('onetime')

module.exports = function runSox(opts, callback) {
if (!opts || typeof opts !== 'object') throw new Error('options must be an object')
if (!opts.inputFile) throw new Error('options.inputFile is a required parameter')
if (!opts.outputFile) throw new Error('options.outputFile is a required parameter')

var cb = onetime(callback || function (e) { if (e) throw e })
function runSoxProm(opts){
return new Promise((resolve,reject)=>{

var args = []
.concat(hashToArray(opts.global || []))
.concat(hashToArray(opts.input || []))
.concat(opts.inputFile)
.concat(hashToArray(opts.output || []))
.concat(opts.outputFile)
.concat(opts.effects || [])
.reduce(function (flattened, ele) {
return flattened.concat(ele)
}, [])
if (!opts || typeof opts !== 'object') return reject(new Error('options must be an object'))
if (!opts.inputFile) return reject(new Error('options.inputFile is a required parameter'))
if (!opts.outputFile) return reject(new Error('options.outputFile is a required parameter'))

var sox = spawn(opts.soxPath || 'sox', args)
sox.on('error', cb)
sox.stderr.on('data', function (stderr) {
cb(new Error(stderr))
})
sox.on('close', function (code, signal) {
if (code) {
cb(new Error(signal))
} else {
cb(null, opts.outputFile)
}
const args = []
.concat(hashToArray(opts.global || []))
.concat(hashToArray(opts.input || []))
.concat(opts.inputFile)
.concat(hashToArray(opts.output || []))
.concat(opts.outputFile)
.concat(opts.effects || [])
.reduce((flattened, ele) => flattened.concat(ele), [])

let sox = spawn(opts.soxPath || 'sox', args)
sox.on('error', reject)
sox.stderr.on('data', stderr=>reject(new Error(stderr)))
sox.on('close', (code, signal) => {
if (code) return reject(new Error(signal))
resolve(opts.outputFile)
})
})
}


module.exports = function runSox(opts,cb) {
if(!cb) return runSoxProm(opts);
runSoxProm(opts)
.then(result => cb(null,result))
.catch(cb)
}
Loading