Skip to content

Commit

Permalink
Minor code cleanups (#3)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mewmew authored and wsc1 committed Aug 8, 2018
1 parent e2b05aa commit f5a3f33
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
gen/*.wav
8 changes: 4 additions & 4 deletions form.go
Expand Up @@ -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.
Expand All @@ -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}
}
37 changes: 14 additions & 23 deletions freq/t.go
Expand Up @@ -10,7 +10,7 @@ import (
"time"
)

// Type Frequency represents a frequency.
// T represents a frequency.
type T int64

// Different levels of "ouch".
Expand All @@ -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 {
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion 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" */
24 changes: 12 additions & 12 deletions pipe.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion sample/codec.go
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions seek.go
Expand Up @@ -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
}

Expand All @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions sink.go
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion source.go
Expand Up @@ -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
Expand Down

0 comments on commit f5a3f33

Please sign in to comment.