Compare commits
3 Commits
5df4b6ff8b
...
b0ce06efb0
Author | SHA1 | Date | |
---|---|---|---|
b0ce06efb0 | |||
e6c413c020 | |||
aacee628dc |
64
audio/audio.go
Normal file
64
audio/audio.go
Normal file
@ -0,0 +1,64 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"github.com/ebitengine/oto/v3"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
hasSetup = false
|
||||
audio Audio
|
||||
)
|
||||
|
||||
type Audio struct {
|
||||
AudioContext *oto.Context
|
||||
}
|
||||
|
||||
func setup() {
|
||||
var ctxOptions = oto.NewContextOptions{
|
||||
SampleRate: 44100,
|
||||
ChannelCount: 2,
|
||||
Format: oto.FormatSignedInt16LE,
|
||||
BufferSize: 0,
|
||||
}
|
||||
|
||||
var ready = make(chan struct{})
|
||||
var err error
|
||||
audio.AudioContext, ready, err = oto.NewContext(&ctxOptions)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
<-ready // Wait for the context to be ready
|
||||
}
|
||||
|
||||
func Play() {
|
||||
if !hasSetup {
|
||||
setup()
|
||||
hasSetup = true
|
||||
}
|
||||
|
||||
var b, err = os.Open("sample.wav")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
var player = audio.AudioContext.NewPlayer(b)
|
||||
player.Play()
|
||||
// Wait for the player to finish
|
||||
for player.IsPlaying() {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
err = player.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
err = b.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
28
main.go
28
main.go
@ -1,32 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fyne.io/fyne/v2/app"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"github.com/ebitengine/oto/v3"
|
||||
"os"
|
||||
"gofin/view"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var a = app.New()
|
||||
var btn = widget.NewButton("Play", func() {
|
||||
var b, _ = os.ReadFile("sample.wav")
|
||||
// create reader from bytes
|
||||
var reader = bytes.NewReader(b)
|
||||
var ctxOptions = oto.NewContextOptions{
|
||||
SampleRate: 44100,
|
||||
ChannelCount: 2,
|
||||
Format: oto.FormatSignedInt16LE,
|
||||
BufferSize: 0,
|
||||
}
|
||||
var ctx, ready, _ = oto.NewContext(&ctxOptions)
|
||||
<-ready // Wait for the context to be ready
|
||||
var player = ctx.NewPlayer(reader)
|
||||
player.Play()
|
||||
},
|
||||
)
|
||||
var w = a.NewWindow("Hello")
|
||||
w.SetContent(btn)
|
||||
w.ShowAndRun()
|
||||
view.CreateView()
|
||||
view.PopulateView()
|
||||
}
|
||||
|
28
view/view.go
Normal file
28
view/view.go
Normal file
@ -0,0 +1,28 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"gofin/audio"
|
||||
)
|
||||
import "fyne.io/fyne/v2/app"
|
||||
|
||||
var (
|
||||
view View
|
||||
)
|
||||
|
||||
type View struct {
|
||||
app fyne.App
|
||||
mainWindow fyne.Window
|
||||
}
|
||||
|
||||
func CreateView() {
|
||||
view.app = app.New()
|
||||
view.mainWindow = view.app.NewWindow("Hello")
|
||||
}
|
||||
|
||||
func PopulateView() {
|
||||
btn := widget.NewButton("Play", audio.Play)
|
||||
view.mainWindow.SetContent(btn)
|
||||
view.mainWindow.ShowAndRun()
|
||||
}
|
Loading…
Reference in New Issue
Block a user