2013-10-23 2 views
0

다음 코드는 Buffer를 사용하여 WAV 파일의 헤더를 스트림에 쓰고 WritableStream에 쓰려고합니다. 어떤 이유nodejs 버퍼가있는 WritableStream에 이진 데이터 쓰기

var fs = require("fs") 

    var samplesLength = 1000; 
    var sampleRate = 44100; 

    var outStream = fs.createWriteStream("zaz.wav") 

    var b = new Buffer(1024)   
    b.write('RIFF', 0); 
    /* file length */  
    b.writeUInt32LE(32 + samplesLength * 2, 4); 
    //b.writeUint32LE(0, 4); 

    b.write('WAVE', 8); 
    /* format chunk identifier */ 
    b.write('fmt ', 12); 

    /* format chunk length */ 
    b.writeUInt32LE(16, 16); 

    /* sample format (raw) */ 
    b.writeUInt16LE(1, 20); 
    /* channel count */ 
    b.writeUInt16LE(2, 22); 
    /* sample rate */ 
    b.writeUInt32LE(sampleRate, 24); 
    /* byte rate (sample rate * block align) */ 
    b.writeUInt32LE(sampleRate * 4, 28); 
    /* block align (channel count * bytes per sample) */ 
    b.writeUInt16LE(4, 32); 
    /* bits per sample */ 
    b.writeUInt16LE(16, 34); 
    /* data chunk identifier */ 
    b.write('data', 36); 
    /* data chunk length */ 

    //b.writeUInt32LE(40, samplesLength * 2);  
    b.writeUInt32LE(40, 0); 


    outStream.write(b.slice(0,50)) 
    outStream.end() 

, 파일의 처음 8 바이트 잘못 :

 
hexdump -C zaz.wav 
00000000 28 00 00 00 f0 07 00 00 57 41 56 45 66 6d 74 20 |(.......WAVEfmt | 
00000010 10 00 00 00 01 00 02 00 44 ac 00 00 10 b1 02 00 |........D.......| 
00000020 04 00 10 00 64 61 74 61 80 5a 57 ac ef 04 00 00 |....data.ZW.....| 
00000030 18 57            |.W| 

이 첫 번째 줄이 있어야한다 :

 

00000000 52 49 46 46 24 00 ff 7f 57 41 56 45 66 6d 74 20 |RIFF$...WAVEfmt | 

업데이트 :

이 줄입니다 결함시 :

,123,210

그것은 있어야 :

b.writeUInt32LE(0, 40); 

답변

2

처음 8 비트 b.writeUInt32LE(40, 0); 의해 기록된다.

= 쓰기 (INT) 40 (= 0x28을) 리틀 엔디안에서은 내가 당신이 원하는 정확히 모르지만이 문제입니다 0

오프셋.

+0

실제로 문제입니다. 고마워! –