TooTallNate / node-wav

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

FileWriter duration (and dataLength) are wrong. Always uses default "dataLength" #10

Open danikenan opened 9 years ago

danikenan commented 9 years ago

We are streaming pcm from the browser to node and saving it using FileWriter. The wav plays, but its header is wrong. Its always the default since we do not know the dataLength at the beginning.

The FileWriter.prototype._onHeader is called at the end, but does not fix anything.

Actually, It seems the child functions (onOpen, onClose, onWrite) within the method are never called so it basically does nothing.

Did you forget

fs.open(this.path, 'r+', onOpen)

at the end?

junaidnasir commented 9 years ago

@danikenan same problem here. btw how are you capturing pcm from browser using binaryjs or socketjs ? i am using binary and having a hard time recording audio as is (frequency changes). what conversion are you doing on browser side and on file writer parameters ? if you can share these that would be helpful

danikenan commented 9 years ago

I have used binaryjs first, and it worked OK. I now use socket.io, since I need multi-platform compatibility and they have examples and client libs in many languages.

The merits of our code on the client is:

navigator.getUserMedia({audio: true, video: false},
                function (stream) {

                    var audioContext = new window.AudioContext(),
                        audioInput = audioContext.createMediaStreamSource(stream);

                    recorder = audioContext.createScriptProcessor(2048, 1, 1);

                    recorder.onaudioprocess = onAudio;

                    audioInput.connect(recorder);

                    recorderDestination = audioContext.destination;

                }, function (err) {

                });

        function onAudio(e) {
            if(!socket){
                return;
            }
            var left = e.inputBuffer.getChannelData(0);
            socket.emit('stream-chunk', convertFloat32ToInt16(left));
        }

Server, inside connection handler is similar to:


    var pending = null;

    socket.on('stream-start', function (data) {
        var fileName = path.join(recordingsDir, new Date().getTime()  + '.wav');
        pending = {
            data: data,
            fileStream: new wav.FileWriter(fileName, {
                channels: 1,
                sampleRate: 8000,
                bitDepth: 16
            })
        };

    });

    socket.on('stream-chunk', function (data) {
        pending.fileStream.write(data);
    });
samuelagm commented 8 years ago

Hello @danikenan having the same issue, have you been able to find a solution ? Thanks.