Esempio n. 1
0
func (v *voice) InitAudio(p audio.Params) {
	audio.Init(v.Pitch, p)
	audio.Init(v.Amp, p)
	v.dt = 1 / p.SampleRate
	v.sqdt = math.Sqrt(v.dt)
	audio.Init(v.RMS, p)
}
Esempio n. 2
0
func Write(v audio.Voice, filename string) {
	f, err := os.Create(filename)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	params := audio.Params{96000}
	const channels = 1
	const bitsPerSample = 32
	w, err := wave.NewWriter(f, &wave.WaveFormatExtensible{Format: wave.WaveFormatEx{
		FormatTag:      wave.WAVE_FORMAT_IEEE_FLOAT,
		Channels:       channels,
		SamplesPerSec:  uint32(params.SampleRate),
		BitsPerSample:  bitsPerSample,
		AvgBytesPerSec: uint32(params.SampleRate * channels * bitsPerSample / 8),
		BlockAlign:     uint16(channels * bitsPerSample / 8),
	}})
	if err != nil {
		panic(err)
	}
	defer w.Close()

	audio.Init(v, params)
	for !v.Done() {
		n, err := w.WriteFloat64Interleaved([][]float64{{v.Sing()}})
		if err != nil {
			panic(err)
		}
		if n != 1 {
			panic("short write")
		}
	}
}
Esempio n. 3
0
func main() {
	w := &window{voices: map[int]*sineVoice{}}
	p := audio.Params{SampleRate: 96000}
	audio.Init(w, p)

	portaudio.Initialize()
	defer portaudio.Terminate()
	s, err := portaudio.OpenDefaultStream(0, 1, p.SampleRate, 64, w.processAudio)
	if err != nil {
		fmt.Println(err)
		return
	}
	if err := s.Start(); err != nil {
		fmt.Println(err)
		return
	}
	defer s.Stop()

	if err := gui.Run(func() {
		gui.NewWindow(w, "beep", func(win *gui.Window) {
			w.Window = win
			gui.SetKeyFocus(w)
		})
	}); err != nil {
		fmt.Println(err)
	}
}
Esempio n. 4
0
func main() {
	flag.Parse()
	if *random {
		*seed = time.Now().UnixNano()
		fmt.Println("seed =", *seed)
	}
	if *seed != math.MinInt64 {
		*random = true
	}
	rand.Seed(*seed)

	portaudio.Initialize()
	defer portaudio.Terminate()

	p := audio.Params{SampleRate: 48000}

	m := audio.MultiVoice{}
	audio.Init(&m, p)
	// for _, x := range [][2]float64{{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {3, 2}, {4, 3}, {5, 4}, {7, 4}, {6, 5}, {7, 5}, {8, 5}, {9, 5}} {
	for xy := 1; xy <= 60; xy++ {
		for x := 2; x <= int(math.Sqrt(float64(xy))); x++ {
			if y := xy / x; x*y == xy && relPrime(x, y) {
				x, y, xy := float64(x), float64(y), float64(xy)
				c := math.Exp(-xy * math.Log2(y/x) / 12)
				f := y / x
				phase := 0.0
				if *random {
					phase = rand.Float64()
				}
				m.Add(newSineBeat(.5*c, 128*f, 1/xy, phase, .1/f))
			}
		}
	}

	s, err := portaudio.OpenDefaultStream(0, 1, p.SampleRate, 512, func(out []float32) {
		for i := range out {
			out[i] = float32(audio.Saturate(m.Sing()))
		}
	})
	if err != nil {
		panic(err)
	}
	s.Start()
	fmt.Println("Press Enter to stop.")
	fmt.Scanln()
	s.Stop()
}
Esempio n. 5
0
func (r *reverb) InitAudio(p audio.Params) {
	r.params = p
	r.streams = nil
	for i := 0; i < 30; i++ {
		s := &grainStream{t: 1}
		r.streams = append(r.streams, s)
	}
	r.buf = make([]float64, int(p.SampleRate))
	r.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
	r.dcFilter.InitAudio(p)
	r.fbRMS = audio.NewRMS(.5)
	r.fbRMS.InitAudio(p)
	r.outRMS = audio.NewRMS(.5)
	r.outRMS.InitAudio(p)
	r.limiter = audio.NewLimiter(.25, 1)
	audio.Init(r.limiter, p)

	r.Decay.InitAudio(p)
	r.Sustain.InitAudio(p)
	r.Dry.InitAudio(p)
	r.Wet.InitAudio(p)
}
Esempio n. 6
0
func (s *song) InitAudio(p audio.Params) {
	audio.Init(&s.EventDelay, p)
	s.EventDelay.Delay(0, s.next)
	audio.Init(&s.MultiVoice, p)
}
Esempio n. 7
0
func (s *song) addTone() {
	// TODO: avoid duplicate freqs
	t := newTone(s.next())
	audio.Init(t, s.Params)
	s.tones = append(s.tones, t)
}