You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.3KB

  1. package rtaudio
  2. import (
  3. "log"
  4. "math"
  5. "time"
  6. )
  7. func ExampleCompiledAPI() {
  8. log.Println("RtAudio version: ", Version())
  9. for _, api := range CompiledAPI() {
  10. log.Println("Compiled API: ", api)
  11. }
  12. }
  13. func ExampleRtAudio_Devices() {
  14. audio, err := Create(APIUnspecified)
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. defer audio.Destroy()
  19. devices, err := audio.Devices()
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. for _, d := range devices {
  24. log.Printf("Audio device: %#v\n", d)
  25. }
  26. }
  27. func ExampleRtAudio_Open() {
  28. const (
  29. sampleRate = 44100
  30. bufSz = 512
  31. freq = 440.0
  32. )
  33. phase := 0.0
  34. audio, err := Create(APIUnspecified)
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. defer audio.Destroy()
  39. params := StreamParams{
  40. DeviceID: uint(audio.DefaultOutputDevice()),
  41. NumChannels: 2,
  42. FirstChannel: 0,
  43. }
  44. options := StreamOptions{
  45. Flags: FlagsAlsaUseDefault,
  46. }
  47. cb := func(out, in Buffer, dur time.Duration, status StreamStatus) int {
  48. samples := out.Float32()
  49. for i := 0; i < len(samples)/2; i++ {
  50. sample := float32(math.Sin(2 * math.Pi * phase))
  51. phase += freq / sampleRate
  52. samples[i*2] = sample
  53. samples[i*2+1] = sample
  54. }
  55. return 0
  56. }
  57. err = audio.Open(&params, nil, FormatFloat32, sampleRate, bufSz, cb, &options)
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. defer audio.Close()
  62. audio.Start()
  63. defer audio.Stop()
  64. time.Sleep(3 * time.Second)
  65. }