【发布时间】: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
我认为readableObjectMode 和writableObjectMode 会有所帮助,但这并不能解决问题。
知道如何解决这个问题吗?
// 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