From f5a3f33a6367c9b25d9561724de5d0219d36c014 Mon Sep 17 00:00:00 2001 From: Robin Eklind Date: Wed, 8 Aug 2018 22:13:01 +0900 Subject: [PATCH] Minor code cleanups (#3) * sound: move Pipe function to top of pipe.go This is to make the more important code (e.g. exported identifiers) be higher up, similar to how Discard is above discard in sink.go * sound: add trailing period after Source doc comment * sound: remove receiver name from discard as its not used * sound: add .gitignore to ignore generated WAV files * sound: clarify some comments * sound: consistent use of channel form comments Single channel and 2-channel vs. mono-channel and stero. Now MonoCd and MonoDvd have the same usage, as do StereoCd and StereoDvd * sound/freq: use switch to increase readability of freq.T.String Also, remove unnecessary else blocks; i.e. change: if foo { return bar } else { return baz } To: if foo { return bar } return baz * sound/freq: fix typo in RadsPerAt comment and update T comment * sound/gen: add trailing dot in package doc comment * sound/sample: fix minor typo --- .gitignore | 1 + form.go | 8 ++++---- freq/t.go | 37 ++++++++++++++----------------------- gen/doc.go | 2 +- pipe.go | 24 ++++++++++++------------ sample/codec.go | 2 +- seek.go | 6 +++--- sink.go | 8 ++++---- source.go | 2 +- 9 files changed, 41 insertions(+), 49 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c125b43 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +gen/*.wav diff --git a/form.go b/form.go index 3d4aa4c..e49a76c 100644 --- a/form.go +++ b/form.go @@ -32,7 +32,7 @@ func NewForm(f freq.T, c int) Form { return &_form{c: c, f: f} } -// Interface Form specifies the logical content of pcm audio data. data, +// Interface Form specifies the logical content of pcm audio data, // namely the number of channels and the sample rate. Form does not specify // the in-memory layout of pcm audio data such as sample codec or whether or // not the data is channel-interleaved. @@ -49,17 +49,17 @@ func MonoCd() Form { return &_form{c: 1, f: 44100 * freq.Hertz} } -// MonoDvd gives a mono-channel Form at Dvd sample rate (48kHz). +// MonoDvd gives a single channel form at Dvd sample rate (48kHz). func MonoDvd() Form { return &_form{c: 1, f: 48000 * freq.Hertz} } -// StereoCd returns a stereo form at CD sampling rate. +// StereoCd returns a 2-channel form at CD sampling rate. func StereoCd() Form { return &_form{c: 2, f: 44100 * freq.Hertz} } -// StereoDvd gives a 2-channel Form at Dvd sample rate (48kHz). +// StereoDvd gives a 2-channel form at Dvd sample rate (48kHz). func StereoDvd() Form { return &_form{c: 2, f: 48000 * freq.Hertz} } diff --git a/freq/t.go b/freq/t.go index 1e89c53..3c6fba6 100644 --- a/freq/t.go +++ b/freq/t.go @@ -10,7 +10,7 @@ import ( "time" ) -// Type Frequency represents a frequency. +// T represents a frequency. type T int64 // Different levels of "ouch". @@ -26,43 +26,36 @@ const ( // String implements Stringer. func (f T) String() string { - if f < 0 { + switch { + case f < 0: if -f >= 0 { return fmt.Sprintf("-%s", -f) } return "-Inf Hz" - } - if f < MicroHertz { + case f < MicroHertz: return fmt.Sprintf("%dnHz", f) - } - if f < MilliHertz { + case f < MilliHertz: if f%MicroHertz == 0 { return fmt.Sprintf("%.3f\u00B5Hz", float64(f)/float64(MicroHertz)) - } else { - return fmt.Sprintf("%.6f\u00B5Hz", float64(f)/float64(MicroHertz)) } - } - if f < Hertz { + return fmt.Sprintf("%.6f\u00B5Hz", float64(f)/float64(MicroHertz)) + case f < Hertz: if f%MilliHertz == 0 { return fmt.Sprintf("%.3fmiHz", float64(f)/float64(MilliHertz)) } else if f%MicroHertz == 0 { return fmt.Sprintf("%.6fmiHz", float64(f)/float64(MilliHertz)) - } else { - return fmt.Sprintf("%.9fmiHz", float64(f)/float64(MilliHertz)) } - } - if f < KiloHertz { + return fmt.Sprintf("%.9fmiHz", float64(f)/float64(MilliHertz)) + case f < KiloHertz: if f%Hertz == 0 { return fmt.Sprintf("%.0fHz", float64(f)/float64(Hertz)) } else if f%MilliHertz == 0 { return fmt.Sprintf("%.3fHz", float64(f)/float64(Hertz)) } else if f%MicroHertz == 0 { return fmt.Sprintf("%.6fHz", float64(f)/float64(Hertz)) - } else { - return fmt.Sprintf("%.9fHz", float64(f)/float64(Hertz)) } - } - if f < MegaHertz { + return fmt.Sprintf("%.9fHz", float64(f)/float64(Hertz)) + case f < MegaHertz: if f%KiloHertz == 0 { return fmt.Sprintf("%.0fkHz", float64(f)/float64(KiloHertz)) } else if f%Hertz == 0 { @@ -71,11 +64,9 @@ func (f T) String() string { return fmt.Sprintf("%.6fkHz", float64(f)/float64(KiloHertz)) } else if f%MicroHertz == 0 { return fmt.Sprintf("%.9fkHz", float64(f)/float64(KiloHertz)) - } else { - return fmt.Sprintf("%.12fkHz", float64(f)/float64(KiloHertz)) } - } - if f < GigaHertz { + return fmt.Sprintf("%.12fkHz", float64(f)/float64(KiloHertz)) + case f < GigaHertz: return fmt.Sprintf("%.3fmgHz", float64(f)/float64(MegaHertz)) } return fmt.Sprintf("%.3fgHz", float64(f)/float64(GigaHertz)) @@ -114,7 +105,7 @@ func (f T) Phase(d time.Duration) float64 { return 2.0 * math.Pi * (math.Mod(df, p) / p) } -// RadPerAt gives the radians per sample of a signal at frequency f sampled at +// RadsPerAt gives the radians per sample of a signal at frequency f sampled at // frequency r. func (f T) RadsPerAt(s T) float64 { d := float64(f) / float64(s) diff --git a/gen/doc.go b/gen/doc.go index 83a1561..887ac5e 100644 --- a/gen/doc.go +++ b/gen/doc.go @@ -1,5 +1,5 @@ // Copyright 2018 The ZikiChomgo Authors. All rights reserved. Use of this source // code is governed by a license that can be found in the License file. -// Package gen provides audio generators +// Package gen provides audio generators. package gen /* import "zikichombo.org/sound/gen" */ diff --git a/pipe.go b/pipe.go index a96826a..c4af663 100644 --- a/pipe.go +++ b/pipe.go @@ -8,6 +8,18 @@ import ( "sync" ) +// Pipe creates a pair of Source and Sink such that writes to the sink +// are passed through to reads from the Source. +// +// The returned source, sink are safe for use in multiple goroutines. +func Pipe(v Form) (Source, Sink) { + pC := make(chan *packet) + doneC := make(chan struct{}) + nC := make(chan int) + p := &pipe{Form: v, pC: pC, doneC: doneC, nC: nC} + return p, p +} + type pipe struct { Form wMu sync.Mutex @@ -120,15 +132,3 @@ func copyFrames(dst *packet, src *packet, nc int) int { } return t } - -// Pipe creates a pair of Source and Sink such that writes to the sink -// are passed through to reads from the Source. -// -// The returned source, sink are safe for use in multiple goroutines. -func Pipe(v Form) (Source, Sink) { - pC := make(chan *packet) - doneC := make(chan struct{}) - nC := make(chan int) - p := &pipe{Form: v, pC: pC, doneC: doneC, nC: nC} - return p, p -} diff --git a/sample/codec.go b/sample/codec.go index cc99269..019099f 100644 --- a/sample/codec.go +++ b/sample/codec.go @@ -129,7 +129,7 @@ func (s Codec) FromFloat64(v float64) uint64 { return bo.Uint64(dst[:]) } -// Decode decodes source samples encoded ito src using codec +// Decode decodes source samples encoded into src using codec // s into a slice of float64s. // // If len(dst)*s.Bytes() > len(src) then Decode panics. diff --git a/seek.go b/seek.go index c7f4ded..febfad5 100644 --- a/seek.go +++ b/seek.go @@ -34,9 +34,9 @@ type SinkSeeker interface { // support. type RandomAccess interface { Seeker - // Receive is specified as in Source. + // Receive is as specified in Source.Receive. Receive(dst []float64) (int, error) - // Send is specified as in Sink. + // Send is as specified as in Sink.Send. Send(src []float64) error } @@ -53,7 +53,7 @@ func When(s Seeker) time.Duration { return s2d(s, s.Pos()) } -// Durations takes a Seeker and returns its total duration in +// Duration takes a Seeker and returns its total duration in // time. func Duration(s Seeker) time.Duration { return s2d(s, s.Len()) diff --git a/sink.go b/sink.go index cd633c1..82a2638 100644 --- a/sink.go +++ b/sink.go @@ -26,18 +26,18 @@ var Discard Sink = &discard{} type discard struct{} -func (d *discard) Send(v []float64) error { +func (*discard) Send(d []float64) error { return nil } -func (d *discard) Close() error { +func (*discard) Close() error { return nil } -func (d *discard) SampleRate() freq.T { +func (*discard) SampleRate() freq.T { return 0 } -func (d *discard) Channels() int { +func (*discard) Channels() int { return 1 } diff --git a/source.go b/source.go index 39324de..e28e265 100644 --- a/source.go +++ b/source.go @@ -3,7 +3,7 @@ package sound -// Source is an interface for a source of samples +// Source is an interface for a source of samples. type Source interface { Form Closer