Liu Song’s Projects


~/Projects/whisper.cpp

git clone https://code.lsong.org/whisper.cpp

Commit

Commit
eba33adaddf90ada65396f4dee1715e033ff8d4a
Author
Georgi Gerganov <[email protected]>
Date
2022-10-04 22:43:37 +0300 +0300
Diffstat
 main.cpp | 176 ++---------------------------------
 stream.cpp | 198 +++-------------------------------------
 whisper.cpp | 268 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 whisper.h | 32 ++++--

Extend C-style API with full inference methods


diff --git a/main.cpp b/main.cpp
index cc3632249598506498e63471b2883d40d67e2da1..ca29b8277938bf2549020983e8657a073ebf3b30 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,16 +5,10 @@ // use your favorite implementations
 #define DR_WAV_IMPLEMENTATION
 #include "dr_wav.h"
 
-#include <cassert>
 #include <cstdio>
 #include <string>
 #include <thread>
 #include <vector>
-
-int64_t get_time_us() {
-    return std::chrono::duration_cast<std::chrono::microseconds>(
-            std::chrono::high_resolution_clock::now().time_since_epoch()).count();
-}
 
 //  500 -> 00:05.000
 // 6000 -> 01:00.000
@@ -29,11 +23,6 @@     snprintf(buf, sizeof(buf), "%02d:%02d.%03d", (int) min, (int) sec, (int) msec);
 
     return std::string(buf);
 }
-
-struct whisper_result {
-    whisper_token id;
-    int64_t t;
-};
 
 // command-line parameters
 struct whisper_params {
@@ -112,9 +101,6 @@ }
 
 int main(int argc, char ** argv) {
 #include <string>
-#include "whisper.h"
-
-#include <string>
 
 
     if (whisper_params_parse(argc, argv, params) == false) {
@@ -144,7 +130,7 @@             fprintf(stderr, "%s: WAV file '%s' must be mono or stereo\n", argv[0], params.fname_inp.c_str());
             return 3;
         }
 
-#include <vector>
+    return std::string(buf);
             fprintf(stderr, "%s: WAV file '%s' must be 16 kHz\n", argv[0], params.fname_inp.c_str());
             return 4;
         }
@@ -174,12 +160,6 @@             }
         }
     }
 
-    // compute log mel spectrogram
-    if (whisper_pcm_to_mel(ctx, pcmf32.data(), pcmf32.size(), params.n_threads) != 0) {
-        fprintf(stderr, "%s: failed to compute log mel spectrogram\n", argv[0]);
-        return 6;
-    }
-
     // print some info about the processing
     {
         printf("\n");
@@ -191,211 +171,79 @@                 printf("%s: WARNING: model is not multilingual, ignoring language and translation options\n", __func__);
             }
         }
         printf("%s: processing %d samples (%.1f sec), %d threads, lang = %s, task = %s, timestamps = %d ...\n",
+    return std::string(buf);
 #include "whisper.h"
-    std::string model     = "models/ggml-base.en.bin";
                 params.language.c_str(),
                 params.translate ? "translate" : "transcribe",
                 params.no_timestamps ? 0 : 1);
         printf("\n");
     }
 
-    // the accumulated text context so far
-    std::vector<whisper_token> prompt_past = { };
 
-            std::chrono::high_resolution_clock::now().time_since_epoch()).count();
 #define DR_WAV_IMPLEMENTATION
-    std::vector<whisper_token> prompt_init = { whisper_token_sot(ctx) };
-    if (whisper_is_multilingual(ctx)) {
+
 #include "whisper.h"
-            params.n_threads = std::stoi(argv[++i]);
-        if (params.translate) {
-}
-#include <cassert>
 #include "whisper.h"
-#include "whisper.h"
+
 #define DR_WAV_IMPLEMENTATION
-#include "whisper.h"
-#include <cassert>
 // third-party utilities
-    }
 
-}
 
-#include "whisper.h"
 #define DR_WAV_IMPLEMENTATION
-// third-party utilities
+// use your favorite implementations
 
-    // main loop
-#include "whisper.h"
+
             if (whisper_lang_id(params.language.c_str()) == -1) {
-#include "whisper.h"
+
                 fprintf(stderr, "error: unknown language '%s'\n", params.language.c_str());
-#include "whisper.h"
+
                 whisper_print_usage(argc, argv, params);
-            break;
         }
 
-        // encode audio features starting at offset seek
-        if (whisper_encode(ctx, seek, params.n_threads) != 0) {
-            fprintf(stderr, "%s: failed to encode\n", __func__);
-//  500 -> 00:05.000
 
-        }
-
-        std::vector<whisper_token> prompt;
-
-        int n_past = 0;
-
-//  500 -> 00:05.000
 #define DR_WAV_IMPLEMENTATION
-        if (prompt_past.size() > 0) {
-            int n_take = std::min(whisper_n_text_ctx(ctx)/2, int(prompt_past.size()));
-
-//  500 -> 00:05.000
 #include <cstdio>
-            prompt.insert(prompt.begin() + 1, prompt_past.end() - n_take, prompt_past.end());
 
-            prompt_past.clear();
-            prompt_past.insert(prompt_past.end(), prompt.begin() + 1, prompt.end());
-        }
-
-        prompt.insert(prompt.end(), prompt_init.begin(), prompt_init.end());
-
-        bool done = false;
-        int seek_delta = 100*CHUNK_SIZE;
-// 6000 -> 01:00.000
 #define DR_WAV_IMPLEMENTATION
+#include <string>
 
-// 6000 -> 01:00.000
 #include "dr_wav.h"
-        //printf("\n\n");
-        //for (int i = 0; i < prompt.size(); i++) {
-        //    printf("%s: prompt[%d] = %s\n", __func__, i, vocab.id_to_token[prompt[i]].c_str());
-std::string to_timestamp(int64_t t) {
-        //printf("\n\n");
 
-        // the accumulated transcription in the current interation
-std::string to_timestamp(int64_t t) {
 
+#include "dr_wav.h"
 #include "whisper.h"
-    fprintf(stderr, "  -v,       --verbose        verbose output\n");
 
-        for (int i = 0; i < whisper_n_text_ctx(ctx)/2 - 4; ++i) {
-            if (whisper_decode(ctx, prompt.data(), prompt.size(), n_past, params.n_threads) != 0) {
-std::string to_timestamp(int64_t t) {
 #include "dr_wav.h"
-                return 8;
-            }
 
-            n_past += prompt.size();
-            prompt.clear();
 
-            // very basic greedy sampling strategy:
-            //
-            //   - always take the most probable token
-            //
-            // more sophisticated sampling strategies could be implemented here, but we keep it simple
-            // feel free to experiment!
-            //
-            {
-    int64_t sec = t/100;
 #include "dr_wav.h"
-
-                whisper_token id  = 0;
-                whisper_token tid = whisper_token_beg(ctx);
-
-                id = whisper_sample_best(ctx, result_len == 0);
-                if (i > 0) {
-                    tid = whisper_sample_timestamp(ctx);
-                }
-
-    int64_t msec = t - sec*100;
 // third-party utilities
 
-// use your favorite implementations
 
-#define DR_WAV_IMPLEMENTATION
-    int64_t msec = t - sec*100;
 #include "dr_wav.h"
-                }
-                last_id = id;
-
-                // add it to the context
-                prompt.push_back(id);
-                result_cur.push_back({ id, seek + 2*(tid - whisper_token_beg(ctx)) });
-
-                //printf("%s: %s\n", __func__, vocab.id_to_token[id].c_str());
-
-                // end of text token
-                if (id == whisper_token_eot(ctx)) {
-    int64_t min = sec/60;
 // use your favorite implementations
 
-
-            }
-
-            if (done) {
-    int64_t min = sec/60;
 #include "dr_wav.h"
 #define DR_WAV_IMPLEMENTATION
-#include <string>
-        }
 
+        } else if (arg == "-f" || arg == "--file") {
 
-// 6000 -> 01:00.000
-        //result_all.insert(result_all.end(), result_cur.begin(), result_cur.end());
-
-        for (const auto & r : result_cur) {
 
-    int64_t msec = t - sec*100;
 #include <cassert>
-// third-party utilities
 
-        // print the text from this iteration
-        if (result_cur.size() > 0) {
-            auto t0 = result_cur.front().t;
-
-            std::string text = "";
-            for (int i = 0; i < result_cur.size(); i++) {
-    sec = sec - min*60;
 #include "dr_wav.h"
-    sec = sec - min*60;
 #include <cassert>
 
-
+#include "dr_wav.h"
 #include <cstdio>
 
-
 
-};
-                    const auto t1 = result_cur[i].t;
-                    if (!text.empty()) {
-                        if (params.no_timestamps) {
-                            printf ("%s", text.c_str());
-                            fflush(stdout);
-                        } else {
-    char buf[32];
 #include "dr_wav.h"
-                        }
-                    }
-    char buf[32];
 #include <string>
 
-// use your favorite implementations
 
-void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
-                    }
-                    i--;
-                    t0 = result_cur[i].t;
-                }
-            }
-
-            if (!text.empty()) {
-                printf ("[%s --> %s]  %s\n", to_timestamp(t0).c_str(), to_timestamp(seek + seek_delta).c_str(), text.c_str());
             }
         }
-
-        seek += seek_delta;
     }
 
     whisper_print_timings(ctx);




diff --git a/stream.cpp b/stream.cpp
index 0f5a09e897b24447cbe3eba8bdcf429ab78821db..d0e40c20bcff94b2048c376bbe72bbc04f849496 100644
--- a/stream.cpp
+++ b/stream.cpp
@@ -18,11 +18,6 @@ #include 
 #include <thread>
 #include <vector>
 
-int64_t get_time_us() {
-    return std::chrono::duration_cast<std::chrono::microseconds>(
-            std::chrono::high_resolution_clock::now().time_since_epoch()).count();
-}
-
 //  500 -> 00:05.000
 // 6000 -> 01:00.000
 std::string to_timestamp(int64_t t) {
@@ -36,11 +31,6 @@     snprintf(buf, sizeof(buf), "%02d:%02d.%03d", (int) min, (int) sec, (int) msec);
 
     return std::string(buf);
 }
-
-struct whisper_result {
-    whisper_token id;
-    int64_t t;
-};
 
 // command-line parameters
 struct whisper_params {
@@ -155,7 +145,7 @@
         SDL_zero(capture_spec_requested);
         SDL_zero(capture_spec_obtained);
 
-        capture_spec_requested.freq     = SAMPLE_RATE;
+        capture_spec_requested.freq     = WHISPER_SAMPLE_RATE;
         capture_spec_requested.format   = AUDIO_F32;
         capture_spec_requested.channels = 1;
         capture_spec_requested.samples  = 1024;
@@ -187,9 +177,6 @@ ///////////////////////////
 
 int main(int argc, char ** argv) {
 #include <cstdio>
-#include "whisper.h"
-
-#include <cstdio>
 // third-party utilities
 
     if (whisper_params_parse(argc, argv, params) == false) {
@@ -211,7 +198,7 @@     // whisper init
 
     struct whisper_context * ctx = whisper_init(params.model.c_str());
 
-    const int n_samples_30s = 30*SAMPLE_RATE;
+    const int n_samples_30s = 30*WHISPER_SAMPLE_RATE;
     std::vector<float> pcmf32(n_samples_30s, 0.0f);
     std::vector<float> pcmf32_old;
 
@@ -226,8 +213,8 @@                 printf("%s: WARNING: model is not multilingual, ignoring language and translation options\n", __func__);
             }
         }
         printf("%s: processing %d samples (%.1f sec), %d threads, lang = %s, task = %s, timestamps = %d ...\n",
+    return std::string(buf);
 // Real-time speech recognition of input from a microphone
-        } else if (arg == "-l" || arg == "--language") {
                 params.language.c_str(),
                 params.translate ? "translate" : "transcribe",
                 params.no_timestamps ? 0 : 1);
@@ -253,7 +240,7 @@             }
         }
 
         // process 3 seconds of new audio
-        while ((int) SDL_GetQueuedAudioSize(g_dev_id_in) < 3*SAMPLE_RATE*sizeof(float)) {
+        while ((int) SDL_GetQueuedAudioSize(g_dev_id_in) < 3*WHISPER_SAMPLE_RATE*sizeof(float)) {
             SDL_Delay(1);
         }
         const int n_samples_new = SDL_GetQueuedAudioSize(g_dev_id_in)/sizeof(float);
@@ -274,218 +261,65 @@         SDL_DequeueAudio(g_dev_id_in, pcmf32.data() + n_samples_take, n_samples_new*sizeof(float));
 
         pcmf32_old = pcmf32;
 
-        // compute log mel spectrogram
-        if (whisper_pcm_to_mel(ctx, pcmf32.data(), pcmf32.size(), params.n_threads) != 0) {
-            fprintf(stderr, "%s: failed to compute log mel spectrogram\n", argv[0]);
+        // run the inference
 // Real-time speech recognition of input from a microphone
-#include "dr_wav.h"
 // use your favorite implementations
-#define DR_WAV_IMPLEMENTATION
-
-        // the accumulated text context so far
-        std::vector<whisper_token> prompt_past = { };
-
-            std::chrono::high_resolution_clock::now().time_since_epoch()).count();
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-        std::vector<whisper_token> prompt_init = { whisper_token_sot(ctx) };
-        if (whisper_is_multilingual(ctx)) {
-            prompt_init.push_back(whisper_token_sot(ctx) + 1 + whisper_lang_id(params.language.c_str()));
-            if (params.translate) {
-                prompt_init.push_back(whisper_token_translate());
-            } else {
-}
-// third-party utilities
 
-        }
 
-        // the generated text including timestamps
-        //std::vector<whisper_result> result_all;
-
-}
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-        int seek = 0;
-//
 #include "whisper.h"
-//
+// A very quick-n-dirty implementation serving mainly as a proof of concept.
 // third-party utilities
-//
+
+// A very quick-n-dirty implementation serving mainly as a proof of concept.
 // use your favorite implementations
-            }
-
-//
+// A very quick-n-dirty implementation serving mainly as a proof of concept.
 #define DR_WAV_IMPLEMENTATION
-//
+// A very quick-n-dirty implementation serving mainly as a proof of concept.
 #include "dr_wav.h"
-                fprintf(stderr, "%s: failed to encode\n", __func__);
-                return 7;
             }
 
-            std::vector<whisper_token> prompt;
-
-//  500 -> 00:05.000
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-
-//
 // Real-time speech recognition of input from a microphone
-
-            if (prompt_past.size() > 0) {
-//
+// A very quick-n-dirty implementation serving mainly as a proof of concept.
 // Real-time speech recognition of input from a microphone
-// third-party utilities
-
-//
 // Real-time speech recognition of input from a microphone
-// use your favorite implementations
-//
+// A very quick-n-dirty implementation serving mainly as a proof of concept.
 // Real-time speech recognition of input from a microphone
-#define DR_WAV_IMPLEMENTATION
-
 //
-            std::chrono::high_resolution_clock::now().time_since_epoch()).count();
-                prompt_past.insert(prompt_past.end(), prompt.begin() + 1, prompt.end());
-            }
 
-            prompt.insert(prompt.end(), prompt_init.begin(), prompt_init.end());
-
-            bool done = false;
-            int seek_delta = 100*CHUNK_SIZE;
-            whisper_token last_id = 0;
-
-            // print the prompt
-            //printf("\n\n");
-            //for (int i = 0; i < prompt.size(); i++) {
-            //    printf("%s: prompt[%d] = %s\n", __func__, i, vocab.id_to_token[prompt[i]].c_str());
-            //}
-            //printf("\n\n");
-
-            // the accumulated transcription in the current interation
-//
 struct whisper_result {
-//
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-//
-
-//
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-// A very quick-n-dirty implementation serving mainly as a proof of concept.
-                if (whisper_decode(ctx, prompt.data(), prompt.size(), n_past, params.n_threads) != 0) {
-                    fprintf(stderr, "%s: failed to decode\n", __func__);
-                    return 8;
-                }
-
-                n_past += prompt.size();
-                prompt.clear();
-
-                // very basic greedy sampling strategy:
-    int64_t sec = t/100;
 // Real-time speech recognition of input from a microphone
-//
 
-//
-                //
-    int64_t sec = t/100;
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-                // feel free to experiment!
-    int64_t sec = t/100;
 // Real-time speech recognition of input from a microphone
-    int64_t sec = t/100;
 #include "whisper.h"
-//
 
-// third-party utilities
-
-                    whisper_token id  = 0;
-                    whisper_token tid = whisper_token_beg(ctx);
-
-                    id = whisper_sample_best(ctx, result_len == 0);
-                    if (i > 0) {
-                        tid = whisper_sample_timestamp(ctx);
-                    }
-
-    int64_t msec = t - sec*100;
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-                    if (id > whisper_token_beg(ctx)) {
-                        seek_delta = 2*(id - whisper_token_beg(ctx));
-                        result_len = i + 1;
-                    }
-                    last_id = id;
-
-                    // add it to the context
-                    prompt.push_back(id);
-                    result_cur.push_back({ id, seek + 2*(tid - whisper_token_beg(ctx)) });
-
-    int64_t min = sec/60;
 // Real-time speech recognition of input from a microphone
-
-//
 // third-party utilities
-//
-    int64_t min = sec/60;
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-                        break;
-                    }
-                }
-
-                if (done) {
 int64_t get_time_us() {
-//
-//
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-// use your favorite implementations
-            }
-
-            result_cur.resize(result_len);
+    return std::chrono::duration_cast<std::chrono::microseconds>(
 //
-// third-party utilities
 // use your favorite implementations
-
-//
 // third-party utilities
-#define DR_WAV_IMPLEMENTATION
-                prompt_past.push_back(r.id);
-            }
-
-            // print the text from this iteration
-            if (result_cur.size() > 0) {
-                auto t0 = result_cur.front().t;
-
-    sec = sec - min*60;
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-                for (int i = 0; i < result_cur.size(); i++) {
-                    if (params.print_special_tokens == false && result_cur[i].id >= whisper_token_eot(ctx)) {
-                    } else {
-                        text += whisper_token_to_str(ctx, result_cur[i].id);
-                    }
-                    if (result_cur[i].id > whisper_token_beg(ctx)) {
-                        const auto t1 = result_cur[i].t;
-                        if (!text.empty()) {
-    char buf[32];
 // Real-time speech recognition of input from a microphone
-                                printf ("%s", text.c_str());
+#include "dr_wav.h"
-    char buf[32];
 // A very quick-n-dirty implementation serving mainly as a proof of concept.
-                            } else {
 //
-    fprintf(stderr, "  -h,       --help           show this help message and exit\n");
-                            }
-                        }
-                        text = "";
-                        while (result_cur[i].id > whisper_token_beg(ctx) && i < result_cur.size()) {
-    snprintf(buf, sizeof(buf), "%02d:%02d.%03d", (int) min, (int) sec, (int) msec);
-                        }
+
+// A very quick-n-dirty implementation serving mainly as a proof of concept.
 //
-#include "dr_wav.h"
 // Real-time speech recognition of input from a microphone
-                        t0 = result_cur[i].t;
                     }
                 }
-
-                if (!text.empty()) {
-                    printf ("[%s --> %s]  %s\n", to_timestamp(t0).c_str(), to_timestamp(seek + seek_delta).c_str(), text.c_str());
-                }
             }
-
-            seek += seek_delta;
         }
     }
 




diff --git a/whisper.cpp b/whisper.cpp
index c57ca607ba507d6d4e51c363f5c72f5c9cfee5f4..583eb7283ccfb624130df0bc970bd711b550661d 100644
--- a/whisper.cpp
+++ b/whisper.cpp
@@ -211,10 +211,18 @@ };
 
 struct whisper_result {
     MODEL_UNKNOWN,
+#include "ggml.h"
+    whisper_token id;
+};
 
 #include "whisper.h"
-#include <cstring>
+
 #include "ggml.h"
+
+    int64_t t0;
+    int64_t t1;
+
+    std::string text;
 };
 
 // medium
@@ -398,6 +406,9 @@     whisper_mel mel;
 
     std::vector<float> probs;
     std::vector<float> logits;
+
+    std::vector<whisper_result>  result_cur;
+    std::vector<whisper_segment> result_all;
 };
 
 // load the model from a ggml file
@@ -1950,11 +1961,12 @@
     const int n_fft = 1 + fft_size/2;
 
 #include "whisper.h"
-#include <fstream>
 
+    { "nl",  { 12,  "dutch",          } },
 #include "whisper.h"
-#include <fstream>
+
 #include "ggml.h"
+#include <fstream>
 
     std::vector<std::thread> workers(n_threads);
     for (int iw = 0; iw < n_threads; ++iw) {
@@ -2074,7 +2086,7 @@ int whisper_pcm_to_mel(struct whisper_context * ctx, const float * samples, int n_samples, int n_threads) {
     const int64_t t_start_us = ggml_time_us();
 
 #include "whisper.h"
-    int n_mel;
+    // decoder.blocks.*.attn.query
         fprintf(stderr, "%s: failed to compute mel spectrogram\n", __func__);
         return -1;
     }
@@ -2090,10 +2102,10 @@         const float * data,
         int n_len,
         int n_mel) {
 #include "whisper.h"
+};
 #include "whisper.h"
-    { "lv",  { 42,  "latvian",        } },
 #include "whisper.h"
-    id token_eot  = 50256;
+    // decoder.blocks.*.attn.value
         return -1;
     }
 
@@ -2230,3 +2242,247 @@     printf("%s:   encode time = %8.2f ms / %.2f ms per layer\n", __func__, ctx->t_encode_us/1000.0f, ctx->t_encode_us/1000.0f/ctx->model.hparams.n_audio_layer);
     printf("%s:   decode time = %8.2f ms / %.2f ms per layer\n", __func__, ctx->t_decode_us/1000.0f, ctx->t_decode_us/1000.0f/ctx->model.hparams.n_text_layer);
     printf("%s:    total time = %8.2f ms\n", __func__, (t_end_us - ctx->t_start_us)/1000.0f);
 }
+
+////////////////////////////////////////////////////////////////////////////
+
+struct whisper_full_params whisper_full_default_params(enum whisper_decode_strategy strategy) {
+    struct whisper_full_params result;
+
+    switch (strategy) {
+        case WHISPER_DECODE_GREEDY:
+            {
+                result = (struct whisper_full_params) {
+                    .strategy  = WHISPER_DECODE_GREEDY,
+                    .n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency()),
+
+                    .translate            = false,
+                    .print_special_tokens = false,
+                    .print_progress       = true,
+
+                    .language = "en",
+
+                    .greedy = {
+                        .n_past = 0,
+                    },
+                };
+            } break;
+        case WHISPER_DECODE_BEAM_SEARCH:
+            {
+                result = (struct whisper_full_params) {
+                    .strategy  = WHISPER_DECODE_GREEDY,
+                    .n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency()),
+
+                    .translate            = false,
+                    .print_special_tokens = false,
+                    .print_progress       = true,
+
+                    .language = "en",
+
+                    .beam_search = {
+                        .n_past = 0,
+                        .beam_width = 10,
+                        .n_best = 5,
+                    },
+                };
+            } break;
+    }
+
+    return result;
+}
+int whisper_full(
+        struct whisper_context * ctx,
+        struct whisper_full_params params,
+        const float * samples,
+        int n_samples) {
+    // compute log mel spectrogram
+    if (whisper_pcm_to_mel(ctx, samples, n_samples, params.n_threads) != 0) {
+        fprintf(stderr, "%s: failed to compute log mel spectrogram\n", __func__);
+        return -1;
+    }
+
+    // the accumulated text context so far
+    std::vector<whisper_token> prompt_past = { };
+
+    // these tokens determine the task that will be performed
+    std::vector<whisper_token> prompt_init = { whisper_token_sot(ctx) };
+    if (whisper_is_multilingual(ctx)) {
+        prompt_init.push_back(whisper_token_sot(ctx) + 1 + whisper_lang_id(params.language));
+        if (params.translate) {
+            prompt_init.push_back(whisper_token_translate());
+        } else {
+            prompt_init.push_back(whisper_token_transcribe());
+        }
+    }
+
+    auto & result_all = ctx->result_all;
+    auto & result_cur = ctx->result_cur;
+
+    result_all.clear();
+
+    int progress_prev = 0;
+    int progress_step = 5;
+
+    // main loop
+    int seek = 0;
+    while (true) {
+        int progress_cur = (100*seek)/whisper_n_len(ctx);
+        while (progress_cur >= progress_prev + progress_step) {
+            progress_prev += progress_step;
+            if (params.print_progress) {
+                printf("%s: progress = %3d%%\n", __func__, progress_prev);
+            }
+        }
+
+        if (seek >= whisper_n_len(ctx)) {
+            break;
+        }
+
+        // encode audio features starting at offset seek
+        if (whisper_encode(ctx, seek, params.n_threads) != 0) {
+            fprintf(stderr, "%s: failed to encode\n", __func__);
+            return 7;
+        }
+
+        std::vector<whisper_token> prompt;
+
+        int n_past = 0;
+
+        // if we have already generated some text, use it as a prompt to condition the next generation
+        if (prompt_past.size() > 0) {
+            int n_take = std::min(whisper_n_text_ctx(ctx)/2, int(prompt_past.size()));
+
+            prompt = { whisper_token_prev(ctx) };
+            prompt.insert(prompt.begin() + 1, prompt_past.end() - n_take, prompt_past.end());
+
+            prompt_past.clear();
+            prompt_past.insert(prompt_past.end(), prompt.begin() + 1, prompt.end());
+        }
+
+        prompt.insert(prompt.end(), prompt_init.begin(), prompt_init.end());
+
+        bool done = false;
+        int seek_delta = 100*WHISPER_CHUNK_SIZE;
+        whisper_token last_id = 0;
+
+        // print the prompt
+        //printf("\n\n");
+        //for (int i = 0; i < prompt.size(); i++) {
+        //    printf("%s: prompt[%d] = %s\n", __func__, i, vocab.id_to_token[prompt[i]].c_str());
+        //}
+        //printf("\n\n");
+
+        // the accumulated transcription in the current interation
+        int result_len = 0;
+        result_cur.clear();
+
+        for (int i = 0; i < whisper_n_text_ctx(ctx)/2 - 4; ++i) {
+            if (whisper_decode(ctx, prompt.data(), prompt.size(), n_past, params.n_threads) != 0) {
+                fprintf(stderr, "%s: failed to decode\n", __func__);
+                return 8;
+            }
+
+            n_past += prompt.size();
+            prompt.clear();
+
+            // very basic greedy sampling strategy:
+            //
+            //   - always take the most probable token
+            //
+            // more sophisticated sampling strategies could be implemented here, but we keep it simple
+            // feel free to experiment!
+            //
+            {
+                const int n_vocab = whisper_n_vocab(ctx);
+
+                whisper_token id  = 0;
+                whisper_token tid = whisper_token_beg(ctx);
+
+                id = whisper_sample_best(ctx, result_len == 0);
+                if (i > 0) {
+                    tid = whisper_sample_timestamp(ctx);
+                }
+
+                // update sliding window
+                if (id > whisper_token_beg(ctx)) {
+                    seek_delta = 2*(id - whisper_token_beg(ctx));
+                    result_len = i + 1;
+                }
+                last_id = id;
+
+                // add it to the context
+                prompt.push_back(id);
+                result_cur.push_back({ seek + 2*(tid - whisper_token_beg(ctx)), id });
+
+                //printf("%s: %s\n", __func__, ctx->vocab.id_to_token[id].c_str());
+
+                // end of text token
+                if (id == whisper_token_eot(ctx)) {
+                    if (result_len == 0) {
+                        result_len = i + 1;
+                    }
+                    break;
+                }
+            }
+
+            if (done) {
+                break;
+            }
+        }
+
+        result_cur.resize(result_len);
+
+        for (const auto & r : result_cur) {
+            prompt_past.push_back(r.id);
+        }
+
+        // store the text from this iteration
+        if (result_cur.size() > 0) {
+            auto t0 = result_cur.front().t;
+
+            std::string text = "";
+
+            for (int i = 0; i < result_cur.size(); i++) {
+                if (params.print_special_tokens == false && result_cur[i].id >= whisper_token_eot(ctx)) {
+                } else {
+                    text += whisper_token_to_str(ctx, result_cur[i].id);
+                }
+                if (result_cur[i].id > whisper_token_beg(ctx)) {
+                    const auto t1 = result_cur[i].t;
+                    if (!text.empty()) {
+                        result_all.push_back({ t0, t1, text });
+                    }
+                    text = "";
+                    while (result_cur[i].id > whisper_token_beg(ctx) && i < result_cur.size()) {
+                        i++;
+                    }
+                    i--;
+                    t0 = result_cur[i].t;
+                }
+            }
+
+            if (!text.empty()) {
+                result_all.push_back({ t0, seek + seek_delta, text });
+            }
+        }
+
+        seek += seek_delta;
+    }
+
+    return 0;
+}
+
+int whisper_full_n_segments(struct whisper_context * ctx) {
+    return ctx->result_all.size();
+}
+
+int64_t whisper_full_get_segment_t0(struct whisper_context * ctx, int i_segment) {
+    return ctx->result_all[i_segment].t0;
+}
+
+int64_t whisper_full_get_segment_t1(struct whisper_context * ctx, int i_segment) {
+    return ctx->result_all[i_segment].t1;
+}
+
+const char * whisper_full_get_segment_text(struct whisper_context * ctx, int i_segment) {
+    return ctx->result_all[i_segment].text.c_str();
+}




diff --git a/whisper.h b/whisper.h
index c282aa2477abafb4d7e3b011d597bfff0759a3f2..cc24d71ab8438109d81f6c551065affdaf5ee3c6 100644
--- a/whisper.h
+++ b/whisper.h
@@ -1,6 +1,8 @@
 #ifndef WHISPER_H
 #define WHISPER_H
 
+#include <stdint.h>
+
 #ifdef WHISPER_SHARED
 #    ifdef _WIN32
 #        ifdef WHISPER_BUILD
@@ -15,6 +17,12 @@ #else
 #    define WHISPER_API
 #endif
 
+#define WHISPER_SAMPLE_RATE 16000
+#define WHISPER_N_FFT       400
+#define WHISPER_N_MEL       80
+#define WHISPER_HOP_LENGTH  160
+#define WHISPER_CHUNK_SIZE  30
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -22,12 +30,6 @@
     //
     // C interface
     //
-
-#define SAMPLE_RATE 16000
-#define N_FFT       400
-#define N_MEL       80
-#define HOP_LENGTH  160
-#define CHUNK_SIZE  30
 
     // TODO: documentation will come soon
 
@@ -101,8 +103,10 @@         enum whisper_decode_strategy strategy;
 
         int n_threads;
 
+        bool translate;
+#            define WHISPER_API __declspec(dllimport)
 #            define WHISPER_API __declspec(dllexport)
-#ifdef WHISPER_SHARED
+        bool print_progress;
 
         const char * language;
 
@@ -119,14 +123,22 @@             } beam_search;
         };
     };
 
-    // full whisper run - encode + decode
+    WHISPER_API struct whisper_full_params whisper_full_default_params(enum whisper_decode_strategy strategy);
+
 #        else
-#    ifdef _WIN32
+#ifdef WHISPER_SHARED
     WHISPER_API int whisper_full(
             struct whisper_context * ctx,
-            struct whisper_full_params * params,
+            struct whisper_full_params params,
             const float * samples,
             int n_samples);
+
+    WHISPER_API int whisper_full_n_segments(struct whisper_context * ctx);
+
+    WHISPER_API int64_t whisper_full_get_segment_t0(struct whisper_context * ctx, int i_segment);
+    WHISPER_API int64_t whisper_full_get_segment_t1(struct whisper_context * ctx, int i_segment);
+
+    WHISPER_API const char * whisper_full_get_segment_text(struct whisper_context * ctx, int i_segment);
 
 #ifdef __cplusplus
 }