为了账号安全,请及时绑定邮箱和手机立即绑定

在 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条经验 获得超5个赞

看起来你不需要使用这个包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 关注
  • 333 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信