wav

package module
v0.0.0-...-49e6d62 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2018 License: Apache-2.0 Imports: 2 Imported by: 0

README

wav

主要功能如下

  • 提供函数生成wav头
  • 提供命令行工具,把pcm音频转成wav格式

参考资料

https://stackoverflow.com/questions/21131595/convert-from-pcm-to-wav-is-it-possible
https://blog.csdn.net/u010011236/article/details/53026127

安装pcm2wav工具

env GOPATH=`pwd` go get github.com/guonaihong/wav/pcm2wav

使用pcm2wav

-ar 指定采样率, -f 指定输入pcm音频 -w 输出wav音频文件名

./pcm2wav -ar 16000 -f ./good.pcm -w good.wav

wav.go

//生成wav结构体 
func New(numChannels uint16, sampleRate uint32, bitsPerSample uint16, wavLen uint32) *WavHead

// 把wav结构体序列化成二进制对象
func (wh *WavHead) Marshal() ([]byte, error)

示例内容(pcm2wav工具源码)

这也是wav库两个对外函数用法示例


sampleRate := flag.Int("ar", 0, "Audio sample rate")
file := flag.String("f", "", "Pcm audio file name")
wavFile := flag.String("w", "", "The name of the wav file to be created")

flag.Parse()

pcmFile, err := os.Open(*file)
if err != nil {
    fmt.Printf("%s\n", err)
    return
}
defer pcmFile.Close()

pcmFi, err := pcmFile.Stat()
if err != nil {
    fmt.Printf("%s\n", err)
    return
}

wavHead := wav.New(uint16(1), uint32(*sampleRate), uint16(16), uint32(pcmFi.Size()))

wavFd, err := os.Create(*wavFile)
if err != nil {
    fmt.Printf("%s\n", err)
    return
}
defer wavFd.Close()

head, err := wavHead.Marshal()
if err != nil {
    fmt.Printf("%s\n", err)
    return
}

wavFd.Write(head)
io.Copy(wavFd, pcmFile)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Subchunk2ID   [4]byte //内容为"data"
	Subchunk2Size uint32  //内容为接下来的正式的数据部分的字节数,其值=NumSamples * NumChannels * BitsPerSample/8
}

type Fmt

type Fmt struct {
	Subchunk1ID   [4]byte //内容为"fmt "
	Subchunk1Size uint32  // Fmt所占字节数,为16
	AudioFormat   uint16  //存储音频的编码格式,pcm为1
	NumChannels   uint16  //通道数, 单通道为1,双通道为2
	SampleRate    uint32  //采样率,如8k, 44.1k等
	ByteRate      uint32  //每秒存储的byte数,其值=SampleRate * NumChannels * BitsPerSample/8
	BlockAlign    uint16  //块对齐大小,其值=NumChannels * BitsPerSample/8
	BitsPerSample uint16  //每个采样点的bit数,一般为8,16,32等。
}
type Head struct {
	ChunkID   [4]byte //内容为"RIFF"
	ChunkSize uint32  //wav文件的字节数, 不包含ChunkID和ChunkSize这8个字节)
	Format    [4]byte //内容为WAVE
}

type WavHead

type WavHead struct {
	Head
	Fmt
	Data
}

func New

func New(numChannels uint16, sampleRate uint32, bitsPerSample uint16, wavLen uint32) *WavHead

func (*WavHead) Marshal

func (wh *WavHead) Marshal() ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL