TooTallNate / node-wav

`Reader` and `Writer` streams for Microsoft WAVE audio files
MIT License
181 stars 37 forks source link

Memory leak when piping in lots of readable streams #7

Closed binarykitchen closed 8 years ago

binarykitchen commented 10 years ago

I am getting the infamous (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. with the following code:

var WavFileWriter = require('wav').FileWriter,

    path          = require('./path'),
    fs            = require('./fs'),
    encoder       = {}

module.exports = encoder

encoder.write = function(options, cb) {

    var wavWriter = new WavFileWriter(options.file, {
        channels:   options.channels,
        sampleRate: options.sampleRate,
        bitDepth:   options.bitDepth
    })

    fs.readdir(options.samplesDir, function(err, files) {
        files.sort(function(a, b) {
            return parseInt(path.basename(a)) < parseInt(path.basename(b)) ? -1 : 1
        }).forEach(function(file) {
            // boom, here the warning appears
            fs.createReadStream(path.join(options.samplesDir, file)).pipe(wavWriter)
        })

        wavWriter.end()

        cb()
    })
}

I am storing the audio samples as files in a directory, numbered from 1 - x. When encoding shall begin, fs.readdir() is scanning these and piping them all into the wavWriter.

What am I doing wrong? Or it is a bug? There isn't much documentation here, so that's why I am asking right here for advice. Many thanks.

1j01 commented 8 years ago

You can use stream-concat:

combinedStream = new StreamConcat(readStreams)
combinedStream.pipe(wavWriter)
binarykitchen commented 8 years ago

oh cool, thanks