在golang中读取* .wav文件

问题描述 投票:-2回答:3

我使用file_get_contents来读取php中的wav文件,我想在Go中使用包github.com/mjibson/go-dsp/wav来完成相同的任务。但是这个包没有任何简单的例子。我是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);
php go wav
3个回答
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


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)
    }
}

希望这可以帮助。


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)
}
© www.soinside.com 2019 - 2024. All rights reserved.