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
9df2ee2
master
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 11struct PcmMonoChunk 12 { 13std ::array < float ,FFT_STEP > mono ; 14 }; 15// PCM buffer with 10 milliseconds of interleaved stereo 16struct PcmStereoChunk 17 { 18std ::array < float ,FFT_STEP * 2 > stereo ; 19 }; 20 21__interface iSampleHandler ; 22 23 constexprHRESULT 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. 27class PcmReader 28 { 29// A small intermediate buffer with PCM data for complete media foundation samples 30AudioBuffer pcm ; 31// Index of the first unconsumed sample in the pcm buffer 32size_t bufferReadOffset = 0 ; 33// Utility object to abstract away mono versus stereo shenanigans 34const iSampleHandler * sampleHandler ; 35// The underlying MF source reader which delivers audio data 36CComPtr < IMFSourceReader > reader ; 37// True after we consumed all available media samples from the reader 38bool m_readerEndOfFile = false; 39// True if this object delivers stereo samples 40bool m_stereoOutput = false; 41// The count of chunks we expect to get from the reader 42size_t m_length = 0 ; 43// Read next sample from the reader, store in the PCM buffer in this class 44HRESULT readNextSample (); 45 46public : 47 48PcmReader (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. 52size_t getLength () const noexcept 53{ 54return m_length; 55} 56 57// True when the stereo flag passed to constructor, and the audio stream actually has 2 or more audio channels 58bool 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 62HRESULT readChunk ( PcmMonoChunk & mono, PcmStereoChunk * stereo ); 63 }; 64}