2022-05-16 20:43:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-16 21:19:00 +02:00
|
|
|
"io/ioutil"
|
2022-05-16 20:43:33 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-04-28 20:44:03 +02:00
|
|
|
"github.com/gopxl/beep"
|
|
|
|
"github.com/gopxl/beep/flac"
|
|
|
|
"github.com/gopxl/beep/mp3"
|
|
|
|
"github.com/gopxl/beep/speaker"
|
|
|
|
"github.com/gopxl/beep/vorbis"
|
|
|
|
"github.com/gopxl/beep/wav"
|
2022-05-16 21:19:00 +02:00
|
|
|
"github.com/h2non/filetype"
|
2022-05-16 20:43:33 +02:00
|
|
|
)
|
|
|
|
|
2022-10-25 18:16:23 +02:00
|
|
|
var firstLoad = true
|
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
func BufferSound(file string) bool {
|
|
|
|
_, ok := streamMap[file]
|
|
|
|
if !ok {
|
2024-04-28 20:44:03 +02:00
|
|
|
fmt.Println("----Not in memory, loading----")
|
2022-05-16 20:43:33 +02:00
|
|
|
f, err := os.Open("./sounds/" + file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Opened file")
|
2022-10-25 18:16:23 +02:00
|
|
|
buf, err := ioutil.ReadFile("sounds/" + string(file))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Fatal error while opening: " + err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
kind, err := filetype.Match(buf)
|
2022-05-16 21:19:00 +02:00
|
|
|
|
2022-10-25 18:16:23 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Fatal error while detecting file type: " + err.Error())
|
|
|
|
return false
|
|
|
|
}
|
2022-05-16 21:19:00 +02:00
|
|
|
|
|
|
|
fmt.Println("File type: " + kind.MIME.Subtype)
|
|
|
|
var streamer beep.StreamSeekCloser
|
|
|
|
var format beep.Format
|
|
|
|
if kind.MIME.Subtype == "mpeg" {
|
2024-04-28 20:44:03 +02:00
|
|
|
streamer, format, err = mp3.Decode(f)
|
2022-05-16 21:19:00 +02:00
|
|
|
} else if kind.MIME.Subtype == "x-wav" {
|
2024-04-28 20:44:03 +02:00
|
|
|
streamer, format, err = wav.Decode(f)
|
2022-05-16 21:19:00 +02:00
|
|
|
} else if kind.MIME.Subtype == "x-flac" {
|
2024-04-28 20:44:03 +02:00
|
|
|
streamer, format, err = flac.Decode(f)
|
2022-05-16 21:19:00 +02:00
|
|
|
} else if kind.MIME.Subtype == "ogg" {
|
2024-04-28 20:44:03 +02:00
|
|
|
streamer, format, err = vorbis.Decode(f)
|
2022-05-16 21:19:00 +02:00
|
|
|
} else {
|
|
|
|
fmt.Println("!!!!! Unsupported file type for " + file)
|
|
|
|
return false
|
|
|
|
}
|
2022-10-25 18:16:23 +02:00
|
|
|
if firstLoad {
|
|
|
|
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
|
|
|
|
firstLoad = false
|
|
|
|
}
|
2024-04-28 20:44:03 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error while decoding file: " + file)
|
|
|
|
fmt.Println("Error: " + fmt.Sprintf("%v", err))
|
|
|
|
return false
|
|
|
|
}
|
2022-05-16 20:43:33 +02:00
|
|
|
|
|
|
|
fmt.Println("Decoded file")
|
2024-04-28 20:44:03 +02:00
|
|
|
fmt.Println("Current file: " + file)
|
|
|
|
fmt.Println("Current streamer: " + fmt.Sprintf("%v", streamer))
|
|
|
|
fmt.Println("Error: " + fmt.Sprintf("%v", err))
|
2022-05-16 20:43:33 +02:00
|
|
|
buffer := beep.NewBuffer(format)
|
|
|
|
buffer.Append(streamer)
|
|
|
|
streamer.Close()
|
|
|
|
fmt.Println("Bufferd file")
|
|
|
|
|
|
|
|
// Save to streamMap
|
|
|
|
streamMap[file] = streamBuf{
|
|
|
|
Streamer: streamer,
|
|
|
|
Format: format,
|
|
|
|
Buffer: buffer,
|
|
|
|
}
|
|
|
|
return (true)
|
|
|
|
} else {
|
|
|
|
return (false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:46:03 +02:00
|
|
|
func PlaySound(file string, index int, loop bool) int {
|
2022-05-16 20:43:33 +02:00
|
|
|
playbacks[index] = playback{
|
|
|
|
File: file,
|
|
|
|
IsLoaded: false,
|
|
|
|
Streamer: nil,
|
|
|
|
Control: nil,
|
2022-05-26 17:46:03 +02:00
|
|
|
Loop: loop,
|
2022-10-13 16:04:51 +02:00
|
|
|
Format: streamMap[file].Format,
|
2022-05-16 20:43:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Playing sound: " + file)
|
|
|
|
var buffer *beep.Buffer
|
|
|
|
BufferSound(file)
|
|
|
|
buffer = streamMap[file].Buffer
|
2022-10-13 16:04:51 +02:00
|
|
|
// streamer := streamMap[file].Streamer
|
2022-05-16 20:43:33 +02:00
|
|
|
|
|
|
|
fmt.Println("Trying to play sound")
|
2022-10-13 16:04:51 +02:00
|
|
|
amountOfLoops := 1
|
|
|
|
if loop {
|
|
|
|
amountOfLoops = -1
|
2023-03-23 21:52:25 +01:00
|
|
|
fmt.Println("Looping sound: " + file)
|
2022-10-13 16:04:51 +02:00
|
|
|
}
|
2023-03-23 21:52:25 +01:00
|
|
|
shot := buffer.Streamer(0, buffer.Len())
|
2022-05-16 20:43:33 +02:00
|
|
|
|
|
|
|
done := make(chan bool)
|
2023-03-23 21:52:25 +01:00
|
|
|
ctrl := &beep.Ctrl{Streamer: beep.Loop(amountOfLoops, shot), Paused: false}
|
2022-05-16 20:43:33 +02:00
|
|
|
|
|
|
|
playbacks[index] = playback{
|
|
|
|
File: file,
|
|
|
|
IsLoaded: true,
|
2022-10-13 16:04:51 +02:00
|
|
|
Streamer: shot,
|
2022-05-16 20:43:33 +02:00
|
|
|
Control: ctrl,
|
2022-05-26 17:46:03 +02:00
|
|
|
Loop: loop,
|
2022-10-13 16:04:51 +02:00
|
|
|
Format: streamMap[file].Format,
|
|
|
|
Done: done,
|
2022-05-16 20:43:33 +02:00
|
|
|
}
|
|
|
|
speaker.Play(ctrl)
|
|
|
|
<-done
|
|
|
|
fmt.Println("Finished playing sound: " + file)
|
2022-10-13 16:04:51 +02:00
|
|
|
delete(playbacks, index)
|
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
return 1
|
|
|
|
}
|