亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 golang 中讀取 *.wav 文件

在 golang 中讀取 *.wav 文件

Go
GCT1015 2023-05-04 16:41:13
我曾經用file_get_contentsPHP 讀取 WAV 文件,我想github.com/mjibson/go-dsp/wav在 Go 中使用包來完成相同的任務。但是沒有關于這個包的任何簡單示例。我是 Go 的新手,不了解它。有沒有人指導我或建議另一種方式?PHP 代碼:$wsdl = 'http://myApi.asmx?WSDL';$client = new SoapClient($wsdl));$data = file_get_contents(public_path() . "/forTest/record.wav");$param = array(  'userName' => '***',  'password' => '***',  'file' => $data);$res = $client->UploadMessage($param);
查看完整描述

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


查看完整回答
反對 回復 2023-05-04
?
紫衣仙女

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)

}


查看完整回答
反對 回復 2023-05-04
?
江戶川亂折騰

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)

    }

}

希望這可以幫助。


查看完整回答
反對 回復 2023-05-04
  • 3 回答
  • 0 關注
  • 620 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號