audio

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2021 License: Apache-2.0 Imports: 5 Imported by: 3

README

Audio support for Go language

LICENSE Build Status Go Report Card GitHub release Coverage Status GoDoc

Qiniu Logo

The package github.com/qiniu/audio is an extensible audio library with simple API for multi platforms in the Go programming language.

Platforms

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • Android
  • iOS
  • Web browsers (Chrome, Firefox, Safari and Edge)
    • GopherJS
    • WebAssembly (Experimental)

Features

  • Pluggable audio decoders. And now it supports the following formats:
    • wav/pcm: import _ "github.com/qiniu/audio/wav"
    • wav/adpcm: import _ "github.com/qiniu/audio/wav/adpcm"
    • mp3: import _ "github.com/qiniu/audio/mp3"
  • Audio encoders (TODO).
  • Convert decoded audio stream.

Example

import (
	"io"
	"os"

	"github.com/hajimehoshi/oto/v2"

	"github.com/qiniu/audio"
	_ "github.com/qiniu/audio/mp3"
	_ "github.com/qiniu/audio/wav"
	_ "github.com/qiniu/audio/wav/adpcm"
)

func playAudio(file string) error {
	f, err := os.Open(file)
	if err != nil {
		return err
	}
	defer f.Close()

	d, _, err := audio.Decode(f)
	if err != nil {
		return err
	}

	c, err := oto.NewContext(d.SampleRate(), d.Channels(), d.BytesPerSample(), 8192)
	if err != nil {
		return err
	}
	defer c.Close()

	p := c.NewPlayer()
	defer p.Close()

	_, err = io.Copy(p, d)
	return err
}

Document

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFormat = errors.New("audio: unknown format")

ErrFormat indicates that decoding encountered an unknown format.

Functions

func RegisterFormat

func RegisterFormat(name, magic string, decode DecodeFunc, decodeCfg DecodeConfigFunc)

RegisterFormat registers an audio format for use by Decode. Name is the name of the format, like "mp3" or "wav". Magic is the magic prefix that identifies the format's encoding. The magic string can contain "?" wildcards that each match any one byte. Decode is the function that decodes the encoded audio. decodeCfg is the function that decodes just its configuration.

Types

type Config

type Config struct {
}

Config holds an audio's configurations.

func DecodeConfig

func DecodeConfig(r io.ReadSeeker) (Config, string, error)

DecodeConfig decodes the basic configurations of an audio that has been encoded in a registered format. The string returned is the format name used during format registration. Format registration is typically done by an init function in the codec-specific package.

type DecodeConfigFunc

type DecodeConfigFunc = func(io.ReadSeeker) (Config, error)

DecodeConfigFunc prototype.

type DecodeFunc

type DecodeFunc = func(io.ReadSeeker) (Decoded, error)

DecodeFunc prototype.

type Decoded

type Decoded interface {
	io.ReadSeeker

	// SampleRate returns the sample rate like 44100.
	SampleRate() int

	// Channels returns the number of channels. One channel is mono playback.
	// Two channels are stereo playback. No other values are supported.
	Channels() int

	// BytesPerSample returns the number of bytes per sample per channel.
	// The usual value is 2. Only values 1 and 2 are supported.
	BytesPerSample() int

	// Length returns the total size in bytes. It returns -1 when the total size is not
	// available. e.g. when the given source is not io.Seeker.
	Length() int64
}

Decoded represents a decoded audio.

func Decode

func Decode(r io.ReadSeeker) (Decoded, string, error)

Decode decodes an audio that has been encoded in a registered format. The string returned is the format name used during format registration. Format registration is typically done by an init function in the codec- specific package.

Directories

Path Synopsis
cmd
wav

Jump to

Keyboard shortcuts

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