【问题标题】:Node streams: how to transform a Buffer stream to write arrays to the output节点流:如何转换 Buffer 流以将数组写入输出
【发布时间】:2021-05-14 04:46:05
【问题描述】:

我有一个 readableStream,其中包含缓冲区数据(音频样本)。
在我的自定义转换中,我读取字节并将音频缓冲区分成单独的通道 (L/R)。

我希望下一个流消费者接收这些转换后的数组以进行进一步的操作。
当我尝试这样做时,它会引发错误:TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Array

我认为readableObjectModewritableObjectMode 会有所帮助,但这并不能解决问题。

知道如何解决这个问题吗?

// Setup the transform
const extractChannels = new ExtractChannelsTransform(
  this.sampleFormat
)

this.stream
  .pipe(extractChannels)
  .pipe(process.stdout)


// ExtractChannelsTransform class
class ExtractChannelsTransform extends Transform {

  constructor(sampleFormat) {
    super({
      objectMode: true,
      readableObjectMode: false,
      writableObjectMode: true
    })
  }

  _transform(chunk, encoding, callback) {

    const channels = ... // chunk bytes are read here and transformed into an array per channel

    callback(null, channels)
  }

}

【问题讨论】:

  • 数组是在管道的什么位置创建的?是 selectedChannels 应该接收数组吗?如果是这样,我相信 readableObjectMode 应该是“true”。
  • 我已经更新了代码 sn-p。这些参数用于从音频缓冲区中读取原始字节,并不重要。数组是在注释 chunk bytes are read here an... 的行上创建的。在那里我读取缓冲区字节,解析它们并将该缓冲区转换为通道数组。
  • 我认为this.stream 会产生缓冲区数据,ExtractChannelsTransform 会将这些缓冲区转换为数组。鉴于上面的示例,您应该翻转 readableObjectMode 的值。如果这不能解决问题,您应该提供一个最小的运行示例。

标签: node.js node-streams


【解决方案1】:

将输出块(通道)转换为有关错误消息的字符串或缓冲区。

https://nodejs.org/api/stream.html#stream_stream

Node.js API 创建的所有流都只对字符串和 Buffer(或 Uint8Array)对象进行操作。

【讨论】:

    猜你喜欢
    • 2011-08-12
    • 2023-03-28
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多