mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-28 12:36:30 +00:00
audio with opus codec
This commit is contained in:
parent
243d547632
commit
f9df886dee
10 changed files with 376 additions and 237 deletions
85
ui/audio.go
85
ui/audio.go
|
|
@ -1,85 +0,0 @@
|
|||
package ui
|
||||
|
||||
import (
|
||||
"github.com/gordonklaus/portaudio"
|
||||
"log"
|
||||
|
||||
"gopkg.in/hraban/opus.v2"
|
||||
// "github.com/xlab/opus-go/opus"
|
||||
)
|
||||
|
||||
|
||||
type Audio struct {
|
||||
stream *portaudio.Stream
|
||||
sampleRate float64
|
||||
outputChannels int
|
||||
channel chan float32
|
||||
}
|
||||
|
||||
func NewAudio() *Audio {
|
||||
a := Audio{}
|
||||
a.channel = make(chan float32, 16000)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a *Audio) Start() error {
|
||||
parameters := portaudio.StreamParameters{}
|
||||
parameters.SampleRate = 44100
|
||||
|
||||
// host, err := portaudio.DefaultHostApi()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// parameters := portaudio.HighLatencyParameters(nil, host.DefaultOutputDevice)
|
||||
stream, err := portaudio.OpenDefaultStream(0, 1, 48000, 0, a.Callback)
|
||||
// stream, err := portaudio.OpenStream(parameters, a.Callback)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := stream.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
a.stream = stream
|
||||
a.sampleRate = parameters.SampleRate
|
||||
// a.outputChannels = parameters.Output.Channels
|
||||
a.outputChannels = 1
|
||||
|
||||
log.Println(a.sampleRate, a.outputChannels, parameters.FramesPerBuffer)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Audio) Stop() error {
|
||||
return a.stream.Close()
|
||||
}
|
||||
|
||||
func (a *Audio) Callback(out []float32) {
|
||||
var output float32
|
||||
log.Println(len(out))
|
||||
for i := range out {
|
||||
if i%a.outputChannels == 1 {
|
||||
select {
|
||||
case sample := <-a.channel:
|
||||
output = sample
|
||||
default:
|
||||
output = 0
|
||||
}
|
||||
}
|
||||
out[i] = output
|
||||
}
|
||||
|
||||
enc, err := opus.NewEncoder(48000, 1, opus.AppVoIP)
|
||||
if err != nil {
|
||||
log.Println("[!] Cannot create audio encoder", err)
|
||||
return
|
||||
}
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.EncodeFloat32(out, data)
|
||||
|
||||
if err != nil {
|
||||
log.Println("[!] Failed to decode")
|
||||
return
|
||||
}
|
||||
data = data[:n]
|
||||
}
|
||||
|
|
@ -27,6 +27,9 @@ const (
|
|||
right2
|
||||
)
|
||||
const NumKeys = 8
|
||||
const SampleRate = 16000
|
||||
const Channels = 1
|
||||
const TimeFrame = 40
|
||||
|
||||
type GameView struct {
|
||||
console *nes.Console
|
||||
|
|
@ -39,10 +42,9 @@ type GameView struct {
|
|||
imageChannel chan *image.RGBA
|
||||
audioChanel chan float32
|
||||
inputChannel chan int
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
func NewGameView(console *nes.Console, title, hash string, imageChannel chan *image.RGBA, audioChanel chan float32, inputChannel chan int) *GameView {
|
||||
gameview := &GameView{
|
||||
console: console,
|
||||
|
|
@ -72,10 +74,7 @@ func (view *GameView) ListenToInputChannel() {
|
|||
|
||||
// Enter enter the game view.
|
||||
func (view *GameView) Enter() {
|
||||
// view.console.SetAudioChannel(view.audio.channel)
|
||||
// view.console.SetAudioSampleRate(view.audio.sampleRate)
|
||||
|
||||
view.console.SetAudioSampleRate(48000)
|
||||
view.console.SetAudioSampleRate(SampleRate)
|
||||
view.console.SetAudioChannel(view.audioChanel)
|
||||
|
||||
// load state
|
||||
|
|
@ -124,11 +123,10 @@ func (view *GameView) updateControllers() {
|
|||
// First 8 keys are player 1
|
||||
var player1Keys [8]bool
|
||||
copy(player1Keys[:], view.keyPressed[:8])
|
||||
|
||||
var player2Keys [8]bool
|
||||
copy(player2Keys[:], view.keyPressed[8:])
|
||||
|
||||
view.console.Controller1.SetButtons(player1Keys)
|
||||
view.console.Controller2.SetButtons(player2Keys)
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue