New is called when the AudioSystem is added to the world.
(w *ecs.World)
| 76 | |
| 77 | // New is called when the AudioSystem is added to the world. |
| 78 | func (a *AudioSystem) New(w *ecs.World) { |
| 79 | switch engo.CurrentBackEnd { |
| 80 | case engo.BackEndMobile: |
| 81 | a.bufsize = 12288 |
| 82 | default: |
| 83 | a.bufsize = 8192 |
| 84 | } |
| 85 | if engo.Headless() { |
| 86 | otoPlayer = &stepPlayer{ |
| 87 | stepStart: make(chan []byte), |
| 88 | stepDone: make(chan struct{}, 1), |
| 89 | } |
| 90 | } else { |
| 91 | if otoPlayer == nil { |
| 92 | c, err := oto.NewContext(SampleRate, channelNum, bytesPerSample, a.bufsize) |
| 93 | if err != nil { |
| 94 | log.Printf("audio error. Unable to create new OtoContext: %v \n\r", err) |
| 95 | } |
| 96 | |
| 97 | otoPlayer = c.NewPlayer() |
| 98 | } else { |
| 99 | closeCh <- struct{}{} |
| 100 | <-loopClosedCh |
| 101 | } |
| 102 | } |
| 103 | // run oto on a separate thread so it doesn't slow down updates |
| 104 | closeCh = make(chan struct{}, 1) |
| 105 | a.pauseCh = make(chan struct{}, 1) |
| 106 | a.restartCh = make(chan struct{}, 1) |
| 107 | a.playerCh = make(chan []*Player, 25) |
| 108 | loopClosedCh = make(chan struct{}) |
| 109 | go func() { |
| 110 | players := make([]*Player, 0) |
| 111 | loop: |
| 112 | for { |
| 113 | select { |
| 114 | case <-closeCh: |
| 115 | break loop |
| 116 | case <-a.pauseCh: |
| 117 | <-a.restartCh |
| 118 | case players = <-a.playerCh: |
| 119 | default: |
| 120 | buf := make([]byte, 2048) |
| 121 | a.read(buf, players) |
| 122 | |
| 123 | if _, err := otoPlayer.Write(buf); err != nil { |
| 124 | log.Printf("error copying to OtoPlayer: %v \r\n", err) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | loopClosedCh <- struct{}{} |
| 129 | }() |
| 130 | masterVolume = 1 |
| 131 | } |
| 132 | |
| 133 | // Add adds an entity to the AudioSystem |
| 134 | func (a *AudioSystem) Add(basic *ecs.BasicEntity, audio *AudioComponent) { |