microphone

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 3 Imported by: 3

README

Microphone GoDoc Go Report Card

Microphone is a small library that takes this Go PortAudio library and wraps its microphone stream in a beep.StreamCloser so that it can be used with everything else in the Beep library.

go get -u github.com/MarkKremer/microphone

Installation

This package requires that you have the PortAudio development headers and libraries installed. On Ubuntu this can be done using:

apt-get install portaudio19-dev

See the PortAudio library for more information.

Licence

MIT

Documentation

Overview

Package microphone provides a wrapper around the PortAudio microphone stream to make it compatible with the beep audio library.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init() error

Init initializes internal datastructures of PortAudio and the host APIs for use.

This method exists as a convenient single point of contact if the client doesn't use any other PortAudio functionality. Otherwise, calling portaudio.Initialize() is recommended instead of calling this method.

func Terminate

func Terminate() error

Terminate deallocates all resources allocated by PortAudio.

This method exists as a convenient single point of contact if the client doesn't use any other PortAudio functionality. Otherwise, calling portaudio.Terminate() is recommended instead of calling this method.

Terminate MUST be called before exiting a program which uses PortAudio. Failure to do so may result in serious resource leaks, such as audio devices not being available until the next reboot.

Types

type Streamer

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

Streamer is an implementation of the beep.StreamCloser interface to provide access to the microphone through the PulseAudio library.

func OpenDefaultStream

func OpenDefaultStream(sampleRate beep.SampleRate, inputChannels int) (s *Streamer, format beep.Format, err error)

OpenDefaultStream opens the default input stream.

Example (RecordWav)
if len(os.Args) < 2 {
	fmt.Println("missing required argument: output file name")
	return
}
fmt.Println("Recording. Press Ctrl-C to stop.")

err := Init()
if err != nil {
	log.Fatal(err)
}
defer Terminate()

stream, format, err := OpenDefaultStream(44100, 1)
if err != nil {
	log.Fatal(err)
}
// Close the stream at the end if it hasn't already been
// closed explicitly.
defer stream.Close()

filename := os.Args[1]
if !strings.HasSuffix(filename, ".wav") {
	filename += ".wav"
}
f, err := os.Create(filename)
if err != nil {
	log.Fatal(err)
}

// Stop the stream when the user tries to quit the program.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
go func() {
	<-sig
	stream.Stop()
	stream.Close()
}()

stream.Start()

// Encode the stream. This is a blocking operation because
// wav.Encode will try to drain the stream. However, this
// doesn't happen until stream.Close() is called.
err = wav.Encode(f, stream, format)
if err != nil {
	log.Fatal(err)
}
Output:

func (*Streamer) Close

func (s *Streamer) Close() error

Close terminates the stream.

func (*Streamer) Err

func (s *Streamer) Err() error

Err returns an error that occurred during streaming. If no error occurred, nil is returned.

func (*Streamer) Start

func (s *Streamer) Start() error

Start commences audio processing.

func (*Streamer) Stop

func (s *Streamer) Stop() error

Stop terminates audio processing (but does not terminate the stream).

func (*Streamer) Stream

func (s *Streamer) Stream(samples [][2]float64) (int, bool)

Stream fills samples with the audio recorded with the microphone. Unless there is an error, this method will wait until samples is filled completely which may involve waiting for the OS to supply the data.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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