Repository: ggerganov/whisper.cpp
File: examples/stream/stream.cpp (C++)

Code before:
        file.write(reinterpret_cast<char *>(&channels), 2);
        file.write(reinterpret_cast<char *>(&sampleRate), 4);
        file.write(reinterpret_cast<char *>(&byteRate), 4);
        file.write(reinterpret_cast<char *>(&blockAlign), 2);
        file.write(reinterpret_cast<char *>(&bitsPerSample), 2);
        file.write("data", 4);
        file.write("\0\0\0\0", 4);    // Placeholder for data size
    }

    void writeData(const float *data, size_t length) {
        for (size_t i = 0; i < length; ++i) {
            int16_t intSample = static_cast<int16_t>(data[i] * 32767);
            file.write(reinterpret_cast<char *>(&intSample), sizeof(int16_t));
            dataSize += sizeof(int16_t);
        }
    }

    ~SimpleWavWriter() {
        if (file.is_open()) {
            file.seekp(4, std::ios::beg);
            int32_t fileSize = 36 + dataSize;
            file.write(reinterpret_cast<char *>(&fileSize), 4);
            file.seekp(40, std::ios::beg);
            file.write(reinterpret_cast<char *>(&dataSize), 4);
            file.close();
        }
    }
};
//  500 -> 00:05.000
// 6000 -> 01:00.000
std::string to_timestamp(int64_t t) {
    int64_t sec = t/100;
    int64_t msec = t - sec*100;
    int64_t min = sec/60;
    sec = sec - min*60;

    char buf[32];
    snprintf(buf, sizeof(buf), "%02d:%02d.%03d", (int) min, (int) sec, (int) msec);

    return std::string(buf);
}

// command-line parameters
struct whisper_params {
    int32_t n_threads  = std::min(4, (int32_t) std::thread::hardware_concurrency());
    int32_t step_ms    = 3000;
    int32_t length_ms  = 10000;
    int32_t keep_ms    = 200;
    int32_t capture_id = -1;
    int32_t max_tokens = 32;
    int32_t audio_ctx  = 0;

Review comment (suggestion):
```suggestion
        if (file.is_open()) {
            file.close();
        }
```

Code after:
        file.write(reinterpret_cast<char *>(&channels), 2);
        file.write(reinterpret_cast<char *>(&sampleRate), 4);
        file.write(reinterpret_cast<char *>(&byteRate), 4);
        file.write(reinterpret_cast<char *>(&blockAlign), 2);
        file.write(reinterpret_cast<char *>(&bitsPerSample), 2);
        file.write("data", 4);
        file.write("\0\0\0\0", 4);    // Placeholder for data size
    }

    void writeData(const float *data, size_t length) {
        for (size_t i = 0; i < length; ++i) {
            int16_t intSample = static_cast<int16_t>(data[i] * 32767);
            file.write(reinterpret_cast<char *>(&intSample), sizeof(int16_t));
            dataSize += sizeof(int16_t);
        }
        if (file.is_open()) {
            file.seekp(4, std::ios::beg);
            int32_t fileSize = 36 + dataSize;
            file.write(reinterpret_cast<char *>(&fileSize), 4);
            file.seekp(40, std::ios::beg);
            file.write(reinterpret_cast<char *>(&dataSize), 4);
            file.seekp(0, std::ios::end);
        }
    }

    ~SimpleWavWriter() {
        if (file.is_open()) {
            file.close();
        }
    }
};
//  500 -> 00:05.000
// 6000 -> 01:00.000
std::string to_timestamp(int64_t t) {
    int64_t sec = t/100;
    int64_t msec = t - sec*100;
    int64_t min = sec/60;
    sec = sec - min*60;

    char buf[32];
    snprintf(buf, sizeof(buf), "%02d:%02d.%03d", (int) min, (int) sec, (int) msec);

    return std::string(buf);
}

// command-line parameters
struct whisper_params {
    int32_t n_threads  = std::min(4, (int32_t) std::thread::hardware_concurrency());
    int32_t step_ms    = 3000;
    int32_t length_ms  = 10000;
    int32_t keep_ms    = 200;