Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.38 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "github.com/gordonklaus/portaudio"
  5.     "github.com/dalloriam/synthia/modular"
  6.     "github.com/dalloriam/synthia"
  7. )
  8.  
  9. const (
  10.     bufferSize = 512 // Size of the audio buffer.
  11.     sampleRate = 44100 // Audio sample rate.
  12.     audioChannelCount = 2 // Stereo.
  13.     mixerChannelCount = 2 // Three oscillators.
  14. )
  15.  
  16. type AudioBackend struct {
  17.     params portaudio.StreamParameters
  18. }
  19.  
  20. func (b *AudioBackend) Start(callback func(in []float32, out [][]float32)) error {
  21.     stream, err := portaudio.OpenStream(b.params, callback)
  22.     if err != nil {
  23.         return err
  24.     }
  25.  
  26.     return stream.Start()
  27. }
  28.  
  29. func (b *AudioBackend) FrameSize() int {
  30.     return b.params.FramesPerBuffer
  31. }
  32.  
  33. func newBackend() *AudioBackend {
  34.     // Quick-and-dirty way to initialize portaudio
  35.     if err := portaudio.Initialize(); err != nil {
  36.         panic(err)
  37.     }
  38.  
  39.     devices, err := portaudio.Devices()
  40.     if err != nil {
  41.         panic(err)
  42.     }
  43.     inDevice, outDevice := devices[0], devices[1]
  44.  
  45.     params := portaudio.LowLatencyParameters(inDevice, outDevice)
  46.  
  47.     params.Input.Channels = 1
  48.     params.Output.Channels = audioChannelCount
  49.  
  50.     params.SampleRate = float64(sampleRate)
  51.     params.FramesPerBuffer = bufferSize
  52.  
  53.     return &AudioBackend{params}
  54. }
  55.  
  56. func main() {
  57.  
  58.     backend := newBackend()
  59.  
  60.     // Set tempo to 60 bpm
  61.     clock := modular.NewClock()
  62.     clock.Tempo.SetValue(60)
  63.  
  64.     // Set the sequence to a C Major scale
  65.     seq := modular.NewSequencer([]float64{130.81, 146.83, 164.1, 174.61, 196, 220, 246.94, 261.63})
  66.     seq.Clock = clock
  67.  
  68.     // Play the sequence in eighth notes
  69.     seq.BeatsPerStep = 1.0 / 2.0
  70.  
  71.     gateSeq := make([]float64, 32)
  72.     for i := 0; i < 32; i++ {
  73.         if i%2 == 0{
  74.             gateSeq[i] = 1
  75.         } else {
  76.             gateSeq[i] = 0
  77.         }
  78.     }
  79.     gate := modular.NewSequencer(gateSeq)
  80.     gate.Clock = clock
  81.     gate.BeatsPerStep = 1.0/12.0
  82.  
  83.     env := modular.NewEnvelope()
  84.     env.Trigger = gate
  85.  
  86.     // Create an oscillator and attach the sequencer to it.
  87.     osc1 := modular.NewOscillator()
  88.     osc1.Frequency.Line = seq
  89.     osc1.Volume.Line = env
  90.  
  91.     // Create the synthesizer with two mixer channels and set it to output to our audio backend.
  92.     synth := synthia.New(mixerChannelCount, bufferSize, backend)
  93.  
  94.     phaser := modular.NewLFO(1, -1)
  95.     phaser.Osc.Frequency.SetValue(1)
  96.  
  97.     // Map two different waves to the two outputs of our mixer.
  98.     synth.Mixer.Channels[0].Input = osc1.Saw
  99.     synth.Mixer.Channels[0].Pan.Line = phaser
  100.  
  101.     // Block until terminated
  102.     select{}
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement