wav

package module
v0.0.0-...-9b8b607 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

wav

Simple package for work with wav

WAV file structure

Offset(byte) Size(byte) Name Description
0 4 chunkId Contains "RIFF" characters in ASCII encoding. It is the beginning of the RIFF chain.
4 4 chunkSize This is the remaining chain size from that position. In other words, this is the file size minus 8, that is, the chunkId and chunkSize fields are excluded.
8 4 format Contains "WAVE" symbols
12 4 subchunk1Id Contains the characters "fmt "
16 4 subchunk1Size 16 for PCM format. This is the remaining size of the chain from this position.
20 2 audioFormat Audio format, list of acceptable formats. For PCM = 1 (that is, Linear Quantization). Values other than 1 indicate some compression format.
22 2 numChannels Number of channels. Mono = 1, Stereo = 2, etc.
24 4 sampleRate Sampling frequency. 8000 Hz, 44100 Hz, etc.
28 4 byteRate The number of bytes transferred per second of playback.
32 2 blockAlign The number of bytes for one sample, including all channels.
34 2 bitsPerSample The number of bits in the sample. The so-called "depth" or accuracy of sound. 8 bits, 16 bits, etc.
36 4 subchunk2Id Contains symbols "data"
40 4 subchunk2Size The number of bytes in the data area.
44 ... data WAV audio data.

Example

import "github.com/geoirb/wav"

Read .wav file

    data, _ := ioutil.ReadFile("path-file.wav")
    wav.NewReader(data)

Create .wav file

	file, _ := os.Create("path-file.wav")
	w := wav.NewWriter(file, 2, 44100, wav.S16LE)
	var data []byte
	for {
		// read audio data
		// ...
		w.Write(data)
		// ...
	}
	w.Close()

Documentation

Index

Constants

View Source
const (
	// S16LE Signed 16 bit Little Endian
	S16LE = 16
	// S24LE Signed 24 bit Little Endian
	S24LE = 24
	// S32LE Signed 32 bit Little Endian
	S32LE = 32
)

Variables

View Source
var (
	ErrSmallSize = errors.New("small size of data")
)

errors

Functions

func ExampleReader

func ExampleReader()

ExampleReader example using wav Reader

func ExampleWriter

func ExampleWriter()

ExampleWriter example using wav Writer

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader wav

func NewReader

func NewReader(data []byte) (r *Reader, err error)

NewReader parse audio data in wav format

func (*Reader) GetAudioFormat

func (r *Reader) GetAudioFormat() uint16

GetAudioFormat audio format

func (*Reader) GetBitsPerSample

func (r *Reader) GetBitsPerSample() uint16

GetBitsPerSample number of bits in the sample

func (*Reader) GetBlockAlign

func (r *Reader) GetBlockAlign() uint16

GetBlockAlign number of bytes for one sample

func (*Reader) GetByteRate

func (r *Reader) GetByteRate() uint32

GetByteRate number of bytes transferred per second of playback

func (*Reader) GetChunkID

func (r *Reader) GetChunkID() []byte

GetChunkID field

func (*Reader) GetChunkSize

func (r *Reader) GetChunkSize() uint32

GetChunkSize size from that position

func (*Reader) GetFormat

func (r *Reader) GetFormat() []byte

GetFormat field

func (*Reader) GetNumChannels

func (r *Reader) GetNumChannels() uint16

GetNumChannels number of channels

func (*Reader) GetSampleRate

func (r *Reader) GetSampleRate() uint32

GetSampleRate sampling frequency

func (*Reader) GetSubchunk1ID

func (r *Reader) GetSubchunk1ID() []byte

GetSubchunk1ID field

func (*Reader) GetSubchunk1Size

func (r *Reader) GetSubchunk1Size() uint32

GetSubchunk1Size field

func (*Reader) GetSubchunk2ID

func (r *Reader) GetSubchunk2ID() []byte

GetSubchunk2ID field

func (*Reader) GetSubchunk2Size

func (r *Reader) GetSubchunk2Size() uint32

GetSubchunk2Size number of bytes in the data area

func (*Reader) Read

func (r *Reader) Read(data []byte) (n int, err error)

Read audio

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer wav file

func NewWriter

func NewWriter(file *os.File, channels uint16, rate, format uint32) (w *Writer)

NewWriter for recording audio data in wav format

func (*Writer) Close

func (w *Writer) Close() (err error)

Close writer and file in wav format

func (*Writer) GetData

func (w *Writer) GetData() []byte

GetData get byte array

func (*Writer) Write

func (w *Writer) Write(data []byte) (n int, err error)

Write audio signal

Jump to

Keyboard shortcuts

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