Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't run Audio with Goncursers #77

Open
Esa824 opened this issue Oct 15, 2023 · 0 comments
Open

Can't run Audio with Goncursers #77

Esa824 opened this issue Oct 15, 2023 · 0 comments

Comments

@Esa824
Copy link

Esa824 commented Oct 15, 2023

When I execute the code without Goncurses, it runs successfully. However, when I run it with Goncurses, it encounters issues. Here are some relevant log entries:
image
And again here is the file:

package main

import (
	"io"
	"log"
	"os"
	"time"

	"github.com/hajimehoshi/go-mp3"
	"github.com/hajimehoshi/oto"
	"github.com/rthornton128/goncurses"
)

func playAudio(file string) {
	f, err := os.Open(file)
	if err != nil {
		log.Printf("Error opening audio file: %v", err)
		return
	}
	defer f.Close()

	d, err := mp3.NewDecoder(f)
	if err != nil {
		log.Printf("Error creating MP3 decoder: %v", err)
		return
	}

	c, err := oto.NewContext(d.SampleRate(), 2, 2, 8192)
	if err != nil {
		log.Printf("Error creating audio context: %v", err)
		return
	}
	defer c.Close()

	p := c.NewPlayer()
	defer p.Close()

	if _, err := p.Write([]byte{0}); err != nil {
		log.Printf("Error playing audio: %v", err)
		return
	}

	_, err = f.Seek(0, 0) // Rewind the audio file
	if err != nil {
		log.Printf("Error rewinding audio: %v", err)
		return
	}

	if _, err := io.Copy(p, d); err != nil {
		log.Printf("Error playing audio: %v", err)
		return
	}
}

func main() {
	stdscr, err := goncurses.Init()
	if err != nil {
		log.Fatal(err)
	}
	defer goncurses.End()

	goncurses.Cursor(0) // Hide the cursor

	// Set up colors (if supported)
	if goncurses.HasColors() {
		goncurses.StartColor()
		goncurses.InitPair(1, goncurses.C_CYAN, goncurses.C_BLACK)
	}

	stdscr.Clear()
	stdscr.Print("Welcome to GoNCurses!\n")
	stdscr.Refresh()

	// Create a new window
	win, err := goncurses.NewWindow(10, 40, 2, 2)
	if err != nil {
		log.Fatal(err)
	}
	defer win.Delete()

	win.Box(0, 0)
	win.MovePrint(1, 1, "This is a GoNCurses window")
	win.Refresh()

	// Start playing audio in a Goroutine
	go playAudio("../../audio/Enter.mp3")

	// Wait for a few seconds to play audio before accepting user input
	time.Sleep(5 * time.Second)

	// Wait for a keypress
	stdscr.Print("Press any key to exit...")
	stdscr.Refresh()
	stdscr.GetChar()
}

I have put the actual working Enter.mp3 and I have provided you with the logs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant