char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
de8656bd9
char/aliceai-t5-moe
1#pragma once 2 3#include "ggml.h" 4#include "clip-model.h" 5 6#include <cstdint> 7#include <vector> 8#include <string> 9 10#define MTMD_INTERNAL_HEADER 11 12struct mtmd_audio_mel { 13int64_t n_len ; 14int64_t n_len_org ; 15int64_t n_mel ; 16 17std ::vector < float > data ; 18}; 19 20struct mtmd_audio_mel_filters { 21int64_t n_mel ; 22int64_t n_fft ; 23 24std ::vector < float > data ; 25}; 26 27// cache for audio processing, each processor instance owns its own cache 28struct mtmd_audio_cache { 29std ::vector < float > sin_vals ; 30std ::vector < float > cos_vals ; 31 32std ::vector < float > hann_window ; 33 34mtmd_audio_mel_filters filters ; 35 36void fill_sin_cos_table (uint32_t n ); 37 38void fill_hann_window (uint32_t length ,bool periodic ); 39 40// Build mel filterbank matrix [n_mel × n_fft_bins] at runtime. 41// n_fft_bins must be (N_fft / 2 + 1). Example: if N_fft=512 -> n_fft_bins=257. 42void fill_mel_filterbank_matrix (int64_t n_mel , 43int64_t n_fft , 44int sample_rate ,// e.g. 16000 45float fmin = 0.0f ,// e.g. 0.0 46float fmax = -1.0f ,// e.g. sr/2; pass -1 for auto 47bool slaney_area_norm = true, 48float scale = 1.0f , 49bool use_htk = false 50 ); 51}; 52 53struct mtmd_audio_preprocessor { 54const clip_hparams & hparams ; 55 56mtmd_audio_preprocessor (const clip_ctx * ctx ): hparams ( * clip_get_hparams ( ctx )) {} 57 58virtual ~ mtmd_audio_preprocessor () = default ; 59virtual void initialize() = 0 ; // NOT thread-safe 60virtual bool preprocess( const float * samples , size_t n_samples , std :: vector < mtmd_audio_mel > & output ) const = 0 ; 61}; 62 63struct mtmd_audio_preprocessor_whisper : mtmd_audio_preprocessor { 64mtmd_audio_preprocessor_whisper ( const clip_ctx * ctx ) : mtmd_audio_preprocessor ( ctx ) {} 65void initialize () override ; 66bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 67 68private : 69mtmd_audio_cache cache; 70}; 71 72struct mtmd_audio_preprocessor_conformer : mtmd_audio_preprocessor { 73mtmd_audio_preprocessor_conformer( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 74void initialize () override ; 75bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 76 77private : 78mtmd_audio_cache cache; 79}; 80 81struct mtmd_audio_preprocessor_granite_speech : mtmd_audio_preprocessor { 82mtmd_audio_preprocessor_granite_speech( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 83void initialize () override ; 84bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 85 86private : 87mtmd_audio_cache cache; 88}; 89 90struct mtmd_audio_preprocessor_gemma4a : mtmd_audio_preprocessor { 91mtmd_audio_preprocessor_gemma4a( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 92void initialize () override ; 93bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 94 95private : 96mtmd_audio_cache cache; 97}; 98 99struct mtmd_audio_preprocessor_gemma4ua : mtmd_audio_preprocessor { 100mtmd_audio_preprocessor_gemma4ua( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 101void initialize () override ; 102bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 103}; 104 105struct mtmd_audio_preprocessor_qwen3a : mtmd_audio_preprocessor { 106mtmd_audio_preprocessor_qwen3a( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 107void initialize () override ; 108bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 109 110private : 111mtmd_audio_cache cache; 112}; 113 114struct mtmd_audio_preprocessor_dots3note : mtmd_audio_preprocessor { 115mtmd_audio_preprocessor_dots3note( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 116void initialize () override ; 117bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 118 119private : 120mtmd_audio_cache cache; 121}; 122 123struct mtmd_audio_preprocessor_mimo_audio : mtmd_audio_preprocessor { 124mtmd_audio_preprocessor_mimo_audio( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 125void initialize () override ; 126bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 127 128private : 129mtmd_audio_cache cache; 130}; 131 132struct mtmd_audio_preprocessor_qwen3tts_spk : mtmd_audio_preprocessor { 133mtmd_audio_preprocessor_qwen3tts_spk( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 134void initialize () override ; 135bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 136 137private : 138mtmd_audio_cache cache; 139}; 140 141// mimi convolves the waveform directly, so this only pads it to a whole number of frames 142struct mtmd_audio_preprocessor_pockettts : mtmd_audio_preprocessor { 143mtmd_audio_preprocessor_pockettts( const clip_ctx * ctx) : mtmd_audio_preprocessor ( ctx ) {} 144void initialize () override {} 145bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 146}; 147 148struct mtmd_audio_preprocessor_parakeet : mtmd_audio_preprocessor { 149mtmd_audio_preprocessor_parakeet (clip_ctx * ctx) : mtmd_audio_preprocessor (ctx) { } 150void initialize () override ; 151bool preprocess ( const float * samples, size_t n_samples, std ::vector < mtmd_audio_mel > & output) const override; 152 153private : 154mtmd_audio_cache cache; 155 156static void worker_thread ( int ith, const float * window_func, int window_size, 157const std ::vector < float > & samples, int n_samples, 158int frame_size, int frame_step, int n_threads, 159int n_fft_bins, 160const mtmd_audio_cache & cache, mtmd_audio_mel & mel); 161}; 162 163// 164// streaming ISTFT - converts spectrogram frames back to audio one frame at a time 165// 166struct mtmd_audio_streaming_istft { 167mtmd_audio_streaming_istft( int n_fft , int hop_length); 168 169// reset streaming state 170void reset (); 171 172// process a single STFT frame (streaming) 173// frame_spectrum: [n_fft_bins x 2] interleaved real/imag 174// returns: up to hop_length samples 175std :: vector < float > process_frame ( const float * frame_spectrum); 176 177// flush remaining samples at end of stream 178std :: vector < float > flush (); 179 180private : 181int n_fft ; 182int hop_length ; 183int n_fft_bins ; 184 185// Own cache for output processing 186mtmd_audio_cache cache ; 187 188// Streaming state 189std :: vector < float > overlap_buffer ; 190std :: vector < float > window_sum_buffer ; 191int padding_to_remove ; 192 193// Working buffers for IFFT 194std :: vector < float > ifft_in ; 195std :: vector < float > ifft_out ; 196};