3 回答

TA貢獻1811條經驗 獲得超6個贊
看起來你不需要使用這個包github.com/mjibson/go-dsp/wav。file_get_contents函數是將文件內容讀入字符串的首選方法。
在 Go 中,你可以這樣做:
package main
import (
"fmt"
"io/ioutil"
)
func public_path() string {
return "/public/path/"
}
func main() {
dat, err := ioutil.ReadFile(public_path() + "/forTest/record.wav")
if err != nil {
fmt.Println(err)
}
fmt.Print(string(dat))
}
https://play.golang.org/p/l9R0940iK50

TA貢獻1839條經驗 獲得超15個贊
給定一個 WAV 文件,這將以浮點音頻曲線的形式返回其有效負載,以及重要的音頻文件詳細信息,如位深度、采樣率和通道數。
如果您只是使用底層 IO 原語之一讀取二進制音頻文件以獲得音頻曲線,您將不得不與自己的位移作斗爭以采摘多字節以及處理大字節序或小字節序int。
您仍然必須了解如何處理來自每個通道的音頻樣本的多通道交錯,這對于單聲道音頻來說不是問題。
package main
import (
"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)
}

TA貢獻1851條經驗 獲得超5個贊
我想你只需要閱讀文件,它是 .wav 還是其他文件都沒有關系。您可以使用go's內置包io/ioutil。
go以下是讀取磁盤文件時應該執行的操作:
package main
import (
"fmt"
"io/ioutil"
"log"
)
func main() {
// Reading .wav file from disk.
fileData, err := ioutil.ReadFile("DISK_RELATIVE_PATH_PREFIX" + "/forTest/record.wav")
// ioutil.ReadFile returns two results,
// first one is data (slice of byte i.e. []byte) and the other one is error.
// If error is having nil value, you got successfully read the file,
// otherwise you need to handle the error.
if err != nil {
// Handle error here.
log.Fatal(err)
} else {
// Do whatever needed with the 'fileData' such as just print the data,
// or send it over the network.
fmt.Print(fileData)
}
}
希望這可以幫助。
- 3 回答
- 0 關注
- 620 瀏覽
添加回答
舉報