Prepend WAV Header in NAudio

天涯浪子 提交于 2019-12-02 22:13:41

问题


I am resampling MP4 (Audio) file into WAV using MediaFoundationReader and ResamplerDmoStream to WaveFormat(16000, 16, 1). After resampling I send the stream to speech API for transcription, but the API fails because the stream does not contain the WAV header. If I save the stream to WAV file (using WaveFileWriter), read the file again and then send the stream to API then the API works because the stream contains the WAV header. Is there a way to prepend the WAV header to the stream after ResamplerDmoStream resampling in order to prevent the time-costly I/O?


回答1:


You can use WaveFileWriter to write to a Stream. So in this case, I'd write to a MemoryStream, and the WaveFileWriter must be disposed in order to properly finalize the WAV headers. But then you'll have an in-memory representation of a WAV file.

One caveat, is that you might end up disposing the MemoryStream when you dispose the WaveFileWriter, so I have a convenience class in NAudio called IgnoreDisposeStream which you can wrap the MemoryStream in to stop it getting disposed before you use it.



来源:https://stackoverflow.com/questions/39328396/prepend-wav-header-in-naudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!