在golang中读取* .wav文件

时间:2018-10-27 13:08:37

标签: php go wav

我使用file_get_contents来读取php中的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 个答案:

答案 0 :(得分:1)

看起来您不需要使用此软件包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

答案 1 :(得分:0)

我认为您只需要读取文件,无论是.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)
    }
}

希望这会有所帮助。

答案 2 :(得分:0)

给出一个wav文件,它会以浮点音频曲线的形式返回其有效载荷,以及基本的音频文件详细信息,例如位深度,采样率和数字通道...如果您只是使用以下方法之一读取二进制音频文件,底层的IO原语才能获得音频曲线,您将不得不自己进行位偏移以获取多字节整数,以及处理大小字节序……因为您仍然必须了解如何处理多重每个通道的音频样本进行多通道交织,这对于单声道音频来说不是问题

package main

import (

    "fmt"
    "os"
    "github.com/youpy/go-wav"
)

func read_wav_file(input_file string, number_of_samples uint32) ([]float64, uint16, uint32, uint16) {

    if number_of_samples == 0 {

        number_of_samples = 268435456 // make default some big number
    }

    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 stens TODO
    //                                                    // 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)
}