func main() { portaudio.Initialize() defer portaudio.Terminate() hs, err := portaudio.HostApis() chk(err) err = tmpl.Execute(os.Stdout, hs) chk(err) }
//Creates a new engine and populates it with the core functions func LoadAudio(e *Engine) *Engine { e = add(e, "portaudio.init", NewCode("portaudio.init", 0, func(e *Engine, c *Thingy) *Engine { portaudio.Initialize() return e })) e = add(e, "portaudio.terminate", NewCode("portaudio.terminate", 0, func(e *Engine, c *Thingy) *Engine { defer portaudio.Terminate() return e })) e = add(e, "portaudio.setData", NewCode("setData", 3, func(ne *Engine, c *Thingy) *Engine { var posString, buffWrap, valString *Thingy posString, ne.dataStack = popStack(ne.dataStack) valString, ne.dataStack = popStack(ne.dataStack) buffWrap, ne.dataStack = popStack(ne.dataStack) var pos, _ = strconv.ParseInt(posString.getSource(), 10, 32) var val, _ = strconv.ParseFloat(valString.getSource(), 32) out := buffWrap._structVal.([][]float32) out[0][pos] = float32(val) return ne })) e = add(e, "portaudio.start", NewCode("portaudio.start", -1, func(e *Engine, c *Thingy) *Engine { //AudioCallbackState = e.RunString("CLEARSTACK", "setup audio callback") AudioCallbackState = e stream, err := portaudio.OpenDefaultStream(0, 2, sampleRate, 0, processAudio) chk(err) stream.Start() chk(err) e.dataStack = pushStack(e.dataStack, NewWrapper(stream)) chk(err) return e })) e = add(e, "portaudio.HostApis", NewCode("portaudio.HostApis", 0, func(e *Engine, c *Thingy) *Engine { var tmpl = template.Must(template.New("").Parse( `[ {{range .}} H[ Name [ {{.Name}} ] {{if .DefaultInputDevice}}DefaultInput [ {{.DefaultInputDevice.Name}} ] {{end}} {{if .DefaultOutputDevice}}DefaultOutput [ {{.DefaultOutputDevice.Name}} ] {{end}} Devices [ {{range .Devices}} H[ Name [ {{.Name}} ] MaxInputChannels [ {{.MaxInputChannels}} ] MaxOutputChannels [ {{.MaxOutputChannels}} ] DefaultLowInputLatency [ {{.DefaultLowInputLatency}} ] DefaultLowOutputLatency [ {{.DefaultLowOutputLatency}} ] DefaultHighInputLatency [ {{.DefaultHighInputLatency}} ] DefaultHighOutputLatency [ {{.DefaultHighOutputLatency}} ] DefaultSampleRate [ {{.DefaultSampleRate}} ] ]H {{end}} ] ]H {{end}} ]`, )) var b bytes.Buffer hs, err := portaudio.HostApis() chk(err) err = tmpl.Execute(&b, hs) chk(err) s := b.String() fmt.Println(s) return e })) return e }
func (m *Meter) Initialize() { hs, err := portaudio.HostApis() if err != nil { fmt.Println("portaudio.HostApis failed:", err) panic(err) } m.Fast = make(map[string]*MeterFast) m.Fast["i"] = &MeterFast{} m.Fast["o"] = &MeterFast{} for _, hai := range hs { apistr := hai.Type.String() if hai.Name != apistr { apistr = fmt.Sprintf("%s \"%s\"", apistr, hai.Name) } all := make(map[string][]*MeterFastDev) for k := range m.Fast { all[k] = make([]*MeterFastDev, 0, len(hai.Devices)) } minLatency := make(map[string]float64) for i, d := range hai.Devices { appendLowLatency := func(k string, numch int, latency float64) { // some devices do not support low latency, indicated by a latency of -1 if numch > 0 && latency > 0 { all[k] = append(all[k], &MeterFastDev{"", i, d, latency}) cur, ok := minLatency[k] if !ok || cur > latency { minLatency[k] = latency } } } appendLowLatency("i", d.MaxInputChannels, d.DefaultLowInputLatency.Seconds()) appendLowLatency("o", d.MaxOutputChannels, d.DefaultLowOutputLatency.Seconds()) } for k, s := range all { for _, d := range s { if (d.Latency - minLatency[k]) >= 1e-3 { continue } d.Name = d.DevInfo.Name switch { case hai.DefaultInputDevice == d.DevInfo && hai.DefaultOutputDevice == d.DevInfo: d.Name = d.Name + " (default_in_out)" case hai.DefaultInputDevice == d.DevInfo: d.Name = d.Name + " (default_in)" case hai.DefaultOutputDevice == d.DevInfo: d.Name = d.Name + " (default_out)" } m.Fast[k].Dev = append(m.Fast[k].Dev, d) } } } for k, s := range m.Fast { if len(s.Dev) < 1 { fmt.Printf("Warning(ch.%s): no known latency on any device. making something up...", k) h, err := portaudio.DefaultHostApi() if err != nil { fmt.Printf("portaudio.DefaultHostApi failed: ", err) panic(err) } defdev := make(map[string]*portaudio.DeviceInfo) defdev["i"] = h.DefaultInputDevice defdev["o"] = h.DefaultOutputDevice m.Fast[k].Dev = append(m.Fast[k].Dev, &MeterFastDev{ Name: "defdev" + k, haiIndex: 0, // Completely made up. DevInfo: defdev[k], Latency: 1000, // Completely made up. }) } } }