yum-archive/TaSTT-Whisper

High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model

git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper

KonstantinWorkaround for the Microsoft’s bug in their MP3 decoder MFT9df2ee2

master
2.1 KiB64 linesraw
1#pragma once
2#include "../Whisper/audioConstants.h"
3#include <mfidl.h>
4#include <mfreadwrite.h>
5#include "AudioBuffer.h"
6#include "../API/iMediaFoundation.cl.h"
7
8namespace Whisper
9{
10	// PCM buffer with 10 milliseconds of single-channel audio
11	struct PcmMonoChunk
12	{
13		std::array<float, FFT_STEP> mono;
14	};
15	// PCM buffer with 10 milliseconds of interleaved stereo
16	struct PcmStereoChunk
17	{
18		std::array<float, FFT_STEP * 2> stereo;
19	};
20
21	__interface iSampleHandler;
22
23	constexpr HRESULT E_EOF = HRESULT_FROM_WIN32( ERROR_HANDLE_EOF );
24
25	// Utility class which reads chunks of FFT_STEP FP32 PCM samples from the MF source reader
26	// The class always delivers mono chunks, and can optionally deliver stereo in a separate buffer.
27	class PcmReader
28	{
29		// A small intermediate buffer with PCM data for complete media foundation samples
30		AudioBuffer pcm;
31		// Index of the first unconsumed sample in the pcm buffer
32		size_t bufferReadOffset = 0;
33		// Utility object to abstract away mono versus stereo shenanigans
34		const iSampleHandler* sampleHandler;
35		// The underlying MF source reader which delivers audio data
36		CComPtr<IMFSourceReader> reader;
37		// True after we consumed all available media samples from the reader
38		bool m_readerEndOfFile = false;
39		// True if this object delivers stereo samples
40		bool m_stereoOutput = false;
41		// The count of chunks we expect to get from the reader
42		size_t m_length = 0;
43		// Read next sample from the reader, store in the PCM buffer in this class
44		HRESULT readNextSample();
45
46	public:
47
48		PcmReader( const iAudioReader* reader );
49
50		// Count of chunks in the MEL spectrogram.
51		// The PCM audio is generally slightly longer than that, due to the incomplete last chunk.
52		size_t getLength() const noexcept
53		{
54			return m_length;
55		}
56
57		// True when the stereo flag passed to constructor, and the audio stream actually has 2 or more audio channels
58		bool outputsStereo() const { return m_stereoOutput; }
59
60		// Load another 10ms chunk from the stream
61		// For the last chunk in the stream, the output buffers are padded with zeros
62		HRESULT readChunk( PcmMonoChunk& mono, PcmStereoChunk* stereo );
63	};
64}