-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bbf869
commit 451b3a8
Showing
2 changed files
with
43 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
package main | ||
|
||
import "os" | ||
import "github.com/faiface/beep/generators" | ||
import "github.com/faiface/beep/speaker" | ||
import "github.com/faiface/beep" | ||
import "strconv" | ||
|
||
import ( | ||
"os" | ||
"github.com/faiface/beep/generators" | ||
"github.com/faiface/beep/speaker" | ||
"github.com/faiface/beep" | ||
"strconv" | ||
) | ||
func main() { | ||
f,_ := strconv.Atoi(os.Args[1]) | ||
speaker.Init(beep.SampleRate(48000), 4800) | ||
s,err := generators.SinTone(beep.SampleRate(48000), f) | ||
if err != nil { | ||
panic(err) | ||
} | ||
speaker.Play(s) | ||
for { | ||
f, _ := strconv.Atoi(os.Args[1]) | ||
speaker.Init(beep.SampleRate(48000), 4800) | ||
s, err := generators.SinTone(beep.SampleRate(48000), f) | ||
if err != nil { | ||
panic(err) | ||
} | ||
speaker.Play(s) | ||
for { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,48 @@ | ||
// tones generator | ||
|
||
|
||
package generators | ||
|
||
import "math" | ||
import "errors" | ||
import . "github.com/faiface/beep" | ||
import ( | ||
"math" | ||
"errors" | ||
. "github.com/faiface/beep" | ||
) | ||
|
||
// simple sinusoid tone generator | ||
type toneStreamer struct { | ||
stat float64 | ||
delta float64 | ||
stat float64 | ||
delta float64 | ||
} | ||
|
||
// create streamer which will produce infinite sinusoid tone with the given frequency | ||
// use other wrappers of this package to change amplitude or add time limit | ||
// sampleRate must be at least two times grater then frequency, otherwise this function will return an error | ||
func SinTone(sr SampleRate, freq int) (Streamer, error) { | ||
if int(sr) / freq < 2 { | ||
return nil,errors.New("faiface beep tone generator: samplerate must be at least 2 times grater then frequency") | ||
} | ||
r := new(toneStreamer) | ||
r.stat = 0.0 | ||
srf := float64(sr) | ||
ff := float64(freq) | ||
steps := srf / ff | ||
r.delta = 1.0 / steps | ||
return r,nil | ||
if int(sr)/freq < 2 { | ||
return nil, errors.New("faiface beep tone generator: samplerate must be at least 2 times grater then frequency") | ||
} | ||
r := new(toneStreamer) | ||
r.stat = 0.0 | ||
srf := float64(sr) | ||
ff := float64(freq) | ||
steps := srf / ff | ||
r.delta = 1.0 / steps | ||
return r, nil | ||
} | ||
|
||
func (c *toneStreamer) nextSample() float64 { | ||
r := math.Sin(c.stat * 2.0 * math.Pi) | ||
_,c.stat = math.Modf(c.stat + c.delta) | ||
return r | ||
r := math.Sin(c.stat * 2.0 * math.Pi) | ||
_, c.stat = math.Modf(c.stat + c.delta) | ||
return r | ||
} | ||
|
||
func (c *toneStreamer) Stream(buf [][2]float64) (int, bool) { | ||
for i := 0; i < len(buf); i++ { | ||
s := c.nextSample() | ||
buf[i] = [2]float64{s,s} | ||
} | ||
return len(buf),true | ||
for i := 0; i < len(buf); i++ { | ||
s := c.nextSample() | ||
buf[i] = [2]float64{s, s} | ||
} | ||
return len(buf), true | ||
} | ||
func (_ *toneStreamer) Err() error { | ||
return nil | ||
} | ||
return nil | ||
} |