Compare commits

..

No commits in common. "b0ce06efb099dcd25f2ab0da20bd6519f0235b01" and "5df4b6ff8bfdd6cffce259383aaab5c5ab2e8838" have entirely different histories.

3 changed files with 25 additions and 95 deletions

View File

@ -1,64 +0,0 @@
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
View File

@ -1,10 +1,32 @@
package main package main
import ( import (
"gofin/view" "bytes"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
"github.com/ebitengine/oto/v3"
"os"
) )
func main() { func main() {
view.CreateView() var a = app.New()
view.PopulateView() 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 File

@ -1,28 +0,0 @@
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()
}