#ifndef ACOUSTIC_ANALYZER_IO_WAV_FILE_SOURCE_H_
#define ACOUSTIC_ANALYZER_IO_WAV_FILE_SOURCE_H_

#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>

namespace acoustic {

/**
 * @brief WAV 文件音频源
 *
 * 支持 16-bit PCM 单声道或多声道 WAV 读取。
 */
class WavFileSource {
public:
    explicit WavFileSource(const std::string& path, int target_sample_rate = 16000);
    ~WavFileSource();

    bool open();
    std::size_t read(std::vector<std::vector<float>>& out, std::size_t max_samples);
    void close();

    std::size_t num_channels() const { return num_channels_; }
    int sample_rate() const { return target_sample_rate_ > 0 ? target_sample_rate_ : file_sample_rate_; }
    int file_sample_rate() const { return file_sample_rate_; }
    std::size_t total_samples() const { return total_samples_; }
    bool is_open() const { return fp_ != nullptr; }

private:
    bool parse_wav_header();
    void resample_if_needed(std::vector<float>& mono, int src_rate, int dst_rate);
    std::string path_;
    int target_sample_rate_;
    std::FILE* fp_;
    uint16_t num_channels_;
    uint32_t file_sample_rate_;
    uint16_t bits_per_sample_;
    long data_start_;
    std::size_t total_samples_;
    std::size_t read_pos_;
};

}  // namespace acoustic

#endif  // ACOUSTIC_ANALYZER_IO_WAV_FILE_SOURCE_H_
