A PX4 based camera pointer

audio.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package pipoint
  16. import (
  17. "fmt"
  18. "os"
  19. "os/exec"
  20. "juju.net.nz/x/pipoint/util"
  21. )
  22. // AudioOut can play files or speech.
  23. type AudioOut struct {
  24. queued chan *exec.Cmd
  25. }
  26. // NewAudioOut creates a new, running audio output.
  27. func NewAudioOut() *AudioOut {
  28. a := &AudioOut{
  29. queued: make(chan *exec.Cmd, 10),
  30. }
  31. go a.run()
  32. return a
  33. }
  34. // Play plays an audio file.
  35. func (a *AudioOut) Play(path string) {
  36. a.queued <- exec.Command("ogg123", path)
  37. }
  38. // Say plays a pre-recorded phrase, or falls back to espeak.
  39. func (a *AudioOut) Say(text string) {
  40. rendered := fmt.Sprintf("phrase/%s.ogg", util.NormText(text))
  41. fi, err := os.Stat(rendered)
  42. if err == nil && fi.Mode().IsRegular() {
  43. a.Play(rendered)
  44. } else {
  45. a.queued <- exec.Command("espeak", text)
  46. }
  47. }
  48. // run executes the commands to play the sounds.
  49. func (a *AudioOut) run() {
  50. for {
  51. cmd := <-a.queued
  52. cmd.Run()
  53. }
  54. }