Coder Social home page Coder Social logo

go-wav's Introduction

go-wav workflow status

A Go library to read/write WAVE(RIFF waveform Audio) Format

Usage

package main

import (
	"flag"
	"fmt"
	"github.com/youpy/go-wav"
	"io"
	"os"
)

func main() {
	infile_path := flag.String("infile", "", "wav file to read")
	flag.Parse()

	file, _ := os.Open(*infile_path)
	reader := wav.NewReader(file)

  	defer file.Close()

	for {
		samples, err := reader.ReadSamples()
		if err == io.EOF {
			break
		}

		for _, sample := range samples {
			fmt.Printf("L/R: %d/%d\n", reader.IntValue(sample, 0), reader.IntValue(sample, 1))
		}
	}
}

Supported format

Format

  • PCM
  • IEEE float (read-only)
  • G.711 A-law (read-only)
  • G.711 µ-law (read-only)

Number of channels

  • 1(mono)
  • 2(stereo)

Bits per sample

  • 32-bit
  • 24-bit
  • 16-bit
  • 8-bit

Documentation

See Also

go-wav's People

Contributors

flowchartsman avatar panapol-p avatar va-stepanov avatar youpy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-wav's Issues

Are float32 files converted to Values [2]int and back to float64?

This is a deal-breaker here for me, in reader.go:

func (r *Reader) FloatValue(sample Sample, channel uint) float64 {
    // XXX
    return float64(r.IntValue(sample, channel)) / math.Pow(2, float64(r.format.BitsPerSample))
}

As is, this drastically distorts the levels of the source audio. What I need are the unmodified original float32 values converted to float64!

Audio duration

Hello there,

is possible to know the duration of the wave file in milliseconds?

WriteSamples is very slow

Hi, when I have big of []wav.sample , WriteSamples is very slow.

process to generate sample finish on 140ms
but WriteSamples take time over 6s to finish

and then I found the solution, change write method from

`func (w *Writer) WriteSamples(samples []Sample) (err error) {
bitsPerSample := w.Format.BitsPerSample
numChannels := w.Format.NumChannels

var i, b uint16
for _, sample := range samples {
	for i = 0; i < numChannels; i++ {
		value := toUint(sample.Values[i], int(bitsPerSample))

		for b = 0; b < bitsPerSample; b += 8 {
			err = binary.Write(w, binary.LittleEndian, uint8((value>>b)&math.MaxUint8))
			if err != nil {
				return
			}
		}
	}
}

return

}`

to

`
func (w *Writer) WriteSamples(samples []Sample) (err error) {
bitsPerSample := w.Format.BitsPerSample
numChannels := w.Format.NumChannels

var i, b uint16
var by []byte
for _, sample := range samples {
	for i = 0; i < numChannels; i++ {
		value := toUint(sample.Values[i], int(bitsPerSample))

		for b = 0; b < bitsPerSample; b += 8 {
			by = append(by, uint8((value>>b)&math.MaxUint8))
		}
	}
}

_, err = w.Write(by)
return

}
`

new WriteSample take time less than 1 sec to finish.

return wav header data

hope to get wav header data from format attr.
so i can detect wheater a 44 or 46-byte header.

Can I record a stream ?

I would like to use NewWriter with nsamples=0, and then when the writer stops update the wav header

Is this possible with go-wav?

add wav header to audio []byte

Hello,

i'm getting audio input and I would like to save it to file but after adding wav header file.

Then save it to .wav file instead of .pcm now..

How to proceed using your library ? Here is my code:

nowS := now.Format("20060102150405")
			pcmFile := fmt.Sprintf("%s/%s_%s.pcm", common.DISK, uid, nowS)
                        //[]byte data add header wav here before save
			ioutil.WriteFile(pcmFile, data, 0644)

can you provide an example about creating a wave file?

Hi,

I'm struggling to create a wave file, I tried this simple program, but it doesn't sound right! (pardon my comicity )

file, err := os.OpenFile("./t.wav", os.O_RDWR|os.O_CREATE, 0666)
        if err != nil {
                fmt.Println(err)
        }
        // 8BitStereo
        var numSamples uint32 = 10
        var numChannels uint16 = 2
        var sampleRate uint32 = 44100
        var bitsPerSample uint16 = 8

        writer := NewWriter(file, numSamples, numChannels, sampleRate, bitsPerSample)
        samples := make([]Sample, numSamples)

        samples[0].Values[0] = 255
        samples[0].Values[1] = 0
        samples[1].Values[0] = 123
        samples[1].Values[1] = 234
        samples[2].Values[0] = 255
        samples[2].Values[1] = 0
        samples[3].Values[0] = 123
        samples[3].Values[1] = 234
        samples[4].Values[0] = 255
        samples[4].Values[1] = 0
        samples[5].Values[0] = 123
        samples[5].Values[1] = 234
        samples[6].Values[0] = 255
        samples[6].Values[1] = 0
        samples[7].Values[0] = 123
        samples[7].Values[1] = 234
        samples[8].Values[0] = 255
        samples[8].Values[1] = 0
        samples[9].Values[0] = 123
        samples[9].Values[1] = 234

        err = writer.WriteSamples(samples)
        if err != nil {
                fmt.Println(err)
        }

        file.Close()

On mac, if I play I just hear a quick bump.

play t.wav 

t.wav:

 File Size: 48        Bit Rate: 8.47M
  Encoding: Unsigned PCM  
  Channels: 2 @ 8-bit    
Samplerate: 44100Hz      
Replaygain: off         
  Duration: 00:00:00.00  

In:100%  00:00:00.00 [00:00:00.00] Out:2     [!=====|=====!] Hd:0.0 Clip:0    
Done.

is the file right, but I didn't specified any sound duration?

Help!

[bug] stereo is not working

after I try to create wav file with 2 channel (stereo)
I found second channel is no data

v0.3.1
image

v0.3.2
image

I would like to fix this feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.