Categorygithub.com/nvisal1/go-wav-codec
repository
0.1.3
Repository: https://github.com/nvisal1/go-wav-codec.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

Go WAV Codec

Latest: v0.1.0

Test codecov made-with-Go GitHub go.mod Go version of a Go module GitHub license Maintained? Open Source? Yes! saythanks

drawing

Table of Contents

Summary

Waveform Audio File Format, commonly shortened to WAV, is an uncompressed audio format. WAV files are usually quite large since they are uncompressed.

This library includes an API that makes it easy to read and write WAV files in Go!

Installation

Using the go wav codec is easy. First, use go get to install the latest version of the library.

go get github.com/nvisal1/go-wav-codec

Next, include go wav codec in your application

import "github.com/nvisal1/go-wav-codec"

Encoder

The Encoder supports writing metadata and audio data to WAV files. This library includes support for writing the following metadata chunks

Chunk IDDescription
LISTIncludes support for type INFO. docs

(as most metadata chunks are frowned upon - they are not widely supported by applications)

Examples

Create a new Encoder

f, err := os.Create("./path/to/your/file")
if err != nil {
    panic(err)
}

defer f.Close()

e, err := NewEncoder(1, 2, 48000, 16, f)
if err != nil {
    panic(err)
}

Write audio data to a new file

a := []int{0,0,0,0} // This is your audio data

err = e.WriteAudioData(a, 0)
if err != nil {
    t.Error(err.Error())
}

err = e.Close()
if err != nil {
    t.Error(err.Error())
}

Write metadata to a new file

a := []int{0,0,0,0} // This is your audio data

err = e.WriteAudioData(a, 0)
if err != nil {
    t.Error(err.Error())
}

ic := &InfoChunk{
		Location:     "",
		Artist:       "artist",
		Software:     "",
		CreationDate: "",
		Copyright:    "",
		Title:        "",
		Engineer:     "",
		Genre:        "",
		Product:      "",
		Source:       "",
		Subject:      "",
		Comments:     "",
		Technician:   "",
		Keywords:     "",
		Medium:       "",
	}

err = e.WriteMetadata(ic)
if err != nil {
    t.Error(err

err = e.Close()
if err != nil {
    t.Error(err.Error())
}

Decoder

The Decoder includes support for reading metadata and audio data from WAV files. This library supports the following metadata chunks

Chunk IDDescription
LISTIncludes support for ADTL and INFO types. docs
SMPLdocs
FACTdocs
PLSTdocs
CUEdocs
INSTdocs

Examples

Create a new Decoder

f, err := os.Open("./path/to/your/file")
if err != nil {
    panic(err)
}

defer f.Close()

d := Decoder.NewDecoder(f)

Read a portion of audio data

a := make([]int, 0)
ad, err := d.ReadAudioData(100, 0)
if err != nil {
    panic(err)
}
a = append(a, ad...)

Read all the audio data in chunks

a := make([]int, 0)
ad, err := d.ReadAudioData(100, 0)
if err != nil {
    t.Error(err.Error())
}
a = append(a, ad...)

for {
    ad, err = d.ReadAudioData(100, 1)
    if err != nil {
        if err == io.EOF {
            break
        }
        panic(err)
    }
    a = append(a, ad...)
}

More Examples

More examples can be found in ./cmd

Expected Chunk Formats

fmt

Size (bytes)DescriptionValue
4Chunk ID"fmt " (this library case insensitive)
4Chunk Size16 (this library does not support extra format bytes)
2Audio Format (Compression Code)1 (this library only supports PCM)
2Number of Channels1 - 65,535
4Sample Rate1 - 0xFFFFFFFF
4Bytes Per Second1 - 0xFFFFFFFF
2Block Align1 - 65,535
2Bits Per Sample (Bit Depth)2 - 65,535
N/AExtra Format Bytes are not supportedN/A

fact

Size (bytes)DescriptionValue
4Chunk ID"fact" (this library case insensitive)
4Chunk Size4 (this library only supports number of samples)
4Number of Samples1 - 0xFFFFFFFF

cue

SizeDescriptionValue
4Chunk ID"cue " (this library case insensitive)
4Chunk Size4 + (NumCuePoints * 24)
4Number of Cue Pointsnumber of cue points
N/ACue Points Start HereN/A
cue point
SizeDescriptionValue
4IDunique identification value
4Positionplay order position
4Data Chunk IDRIFF ID of corresponding data chunk
4Chunk StartByte Offset of Data Chunk
4Block StartByte Offset to sample of First Channel
4Sample OffsetByte Offset to sample byte of First Channel

plst

SizeDescriptionValue
4Chunk ID"plst" (this library case insensitive)
4Chunk SizeNumber of Segments * 12
4Number of Segments1 - 0xFFFFFFFF
segment
SizeDescriptionValue
4Cue Point ID0 - 0xFFFFFFFF
4Length (in samples)1 - 0xFFFFFFFF
4Number of Repeats1 - 0xFFFFFFFF

list

SizeDescriptionValue
4Chunk ID"list" (this library case insensitive)
4Chunk Sizedepends on type
4Type ID"adtl" or "info" (this library case insensitive)

labl

SizeDescriptionValue
4Chunk ID"labl" (this library case insensitive)
4Chunk Sizedepends on text
4Cue Point ID0 - 0xFFFFFFFF
N/ATextN/A

ltxt

SizeDescriptionValue
4Chunk ID"ltxt" (this library case insensitive)
4Chunk Sizedepends on text
4Cue Point ID0 - 0xFFFFFFFF
4Sample Length0 - 0xFFFFFFFF
4Purpose ID0 - 0xFFFFFFFF
2Country0 - 0xFFFF
2Language0 - 0xFFFF
2Dialect0 - 0xFFFF
2Code Page0 - 0xFFFF
N/ATextN/A

note

SizeDescriptionValue
4Chunk ID"note" (this library case insensitive)
4Chunk Sizedepends on text
4Cue Point ID0 - 0xFFFFFFFF
N/ATextN/A

info chunks

Chunk IDDescriptionIs Supported
(by this library)
IARLLocation
IARTArtist
ICMS⬜️
ICMTComments
ICOPCopyright
ICRDCreation Date
ICRP⬜️
IDIM⬜️
IDPI⬜️
IENGEngineer
IGNRGenre
IKEYKeywords
ILGT⬜️
IMEDMedium
INAMTitle
IPLT⬜️
IPRDProduct
ISBJSubject
ISFTSoftware
ISRCSource
ISRF⬜️
ITCHTechnician
ITRK⬜️

smpl

SizeDescriptionValue
4Chunk ID"smpl" (this library case insensitive)
4Chunk Size36 + (Number of Sample Loops * 24) + Sampler Data
4Manufacturer0 - 0xFFFFFFFF
4Product0 - 0xFFFFFFFF
4Sample Period0 - 0xFFFFFFFF
4MIDI Unity Note0 - 127
4MIDI Pitch Fraction0 - 0xFFFFFFFF
4SMPTE Format0, 24, 25, 29, 30
4SMPTE Offset0 - 0xFFFFFFFF
4Number of Sample Loops0 - 0xFFFFFFFF
4Sampler Data0 - 0xFFFFFFFF
N/ASampler Loops Start HereN/A
sampler loop
SizeDescriptionValue
4Cue Point ID0 - 0xFFFFFFFF
4Type0 - 0xFFFFFFFF
4Start0 - 0xFFFFFFFF
4End0 - 0xFFFFFFFF
4Fraction0 - 0xFFFFFFFF
4Play Count0 - 0xFFFFFFFF

inst

SizeDescriptionValue
4Chunk ID"inst" (this library case insensitive)
4Chunk Size7
1Unshifted Note0 - 127
1Fine Tune (dB)-50 - +50
1Gain-64 - +64
1Low Note0 - 127
1High Note0 - 127
1Low Velocity1 - 127
1High Velocity1 - 127

Resources

WebsiteDescriptionLink
musicg-apiWav File FormatHere
recordingblogsList Chunk (of a RIFF file)Here
soundfileWAVE PCM soundfile formatHere

License

Go Wav Codec is released under the MIT license. See LICENSE