Compare commits

...

3 Commits

Author SHA1 Message Date
b0ce06efb0 Abstract main 2024-02-17 00:54:26 -04:00
e6c413c020 View skeleton 2024-02-17 00:54:16 -04:00
aacee628dc Audio skeleton 2024-02-17 00:54:08 -04:00
3 changed files with 95 additions and 25 deletions

64
audio/audio.go Normal file
View 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
View File

@ -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
View 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()
}