紫衣仙女
给定一个 WAV 文件,这将以浮点音频曲线的形式返回其有效负载,以及重要的音频文件详细信息,如位深度、采样率和通道数。如果您只是使用底层 IO 原语之一读取二进制音频文件以获得音频曲线,您将不得不与自己的位移作斗争以采摘多字节以及处理大字节序或小字节序int。您仍然必须了解如何处理来自每个通道的音频样本的多通道交错,这对于单声道音频来说不是问题。package mainimport ( "fmt" "os" "github.com/youpy/go-wav" "math")func read_wav_file(input_file string, number_of_samples uint32) ([]float64, uint16, uint32, uint16) { if number_of_samples == 0 { number_of_samples = math.MaxInt32 } blockAlign := 2 file, err := os.Open(input_file) if err != nil { panic(err) } reader := wav.NewReader(file) wavformat, err_rd := reader.Format() if err_rd != nil { panic(err_rd) } if wavformat.AudioFormat != wav.AudioFormatPCM { panic("Audio format is invalid ") } if int(wavformat.BlockAlign) != blockAlign { fmt.Println("Block align is invalid ", wavformat.BlockAlign) } samples, err := reader.ReadSamples(number_of_samples) // must supply num samples w/o defaults to 2048 // // just supply a HUGE number then actual num is returned wav_samples := make([]float64, 0) for _, curr_sample := range samples { wav_samples = append(wav_samples, reader.FloatValue(curr_sample, 0)) } return wav_samples, wavformat.BitsPerSample, wavformat.SampleRate, wavformat.NumChannels}func main() { input_audio := "/blah/genome_synth_evolved.wav" audio_samples, bits_per_sample, input_audio_sample_rate, num_channels := read_wav_file( input_audio, 0) fmt.Println("num samples ", len(audio_samples)/int(num_channels)) fmt.Println("bit depth ", bits_per_sample) fmt.Println("sample rate ", input_audio_sample_rate) fmt.Println("num channels", num_channels)}