【问题标题】:nodejs write binary data into WritableStream with Buffernodejs用Buffer将二进制数据写入WritableStream
【发布时间】:2013-11-02 03:30:58
【问题描述】:

以下代码尝试将 WAV 文件的标头写入流 使用缓冲区,然后将其写入 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 交流 00 00 10 b1 02 00 |........D........| 00000020 04 00 10 00 64 61 74 61 80 5a 57 ac ef 04 00 00 |....数据.ZW.....| 00000030 18 57 |.W|

第一行应该是:

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

更新:

这条线有问题:

b.writeUInt32LE(40, 0);

必须是:

b.writeUInt32LE(0, 40);

【问题讨论】:

    标签: node.js


    【解决方案1】:

    前 8 位由b.writeUInt32LE(40, 0); 写入。

    = 在偏移 0 处以 Little Endian 写入 (int) 40 (= 0x28)。

    我不知道你到底想要什么,但这就是问题所在。

    【讨论】:

    • 这确实是问题所在。谢谢!
    猜你喜欢
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 2016-01-15
    • 2018-09-04
    • 1970-01-01
    相关资源
    最近更新 更多