mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-17 08:54:19 +00:00
Move record processing to a separate function.
This commit is contained in:
parent
1cf39069c3
commit
0cae3a7c51
2 changed files with 81 additions and 51 deletions
|
|
@ -2,16 +2,14 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"song-recognition/models"
|
||||
"song-recognition/shazam"
|
||||
"song-recognition/spotify"
|
||||
"song-recognition/utils"
|
||||
"song-recognition/wav"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
socketio "github.com/googollee/go-socket.io"
|
||||
"github.com/mdobak/go-xerrors"
|
||||
|
|
@ -30,14 +28,6 @@ func downloadStatus(statusType, message string) string {
|
|||
return string(jsonData)
|
||||
}
|
||||
|
||||
type RecordData struct {
|
||||
Audio string `json:"audio"`
|
||||
Duration float64 `json:"duration"`
|
||||
Channels int `json:"channels"`
|
||||
SampleRate int `json:"sampleRate"`
|
||||
SampleSize int `json:"sampleSize"`
|
||||
}
|
||||
|
||||
func handleTotalSongs(socket socketio.Conn) {
|
||||
logger := utils.GetLogger()
|
||||
ctx := context.Background()
|
||||
|
|
@ -188,57 +178,21 @@ func handleNewRecording(socket socketio.Conn, recordData string) {
|
|||
logger := utils.GetLogger()
|
||||
ctx := context.Background()
|
||||
|
||||
var recData RecordData
|
||||
var recData models.RecordData
|
||||
if err := json.Unmarshal([]byte(recordData), &recData); err != nil {
|
||||
err := xerrors.New(err)
|
||||
logger.ErrorContext(ctx, "Failed to unmarshal record data.", slog.Any("error", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Decode base64 data
|
||||
decodedAudioData, err := base64.StdEncoding.DecodeString(recData.Audio)
|
||||
samples, err := utils.ProcessRecording(&recData, true)
|
||||
if err != nil {
|
||||
err := xerrors.New(err)
|
||||
logger.ErrorContext(ctx, "failed to decode base64 data.", slog.Any("error", err))
|
||||
logger.ErrorContext(ctx, "Failed to process recording.", slog.Any("error", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Save the decoded data to a file
|
||||
channels := recData.Channels
|
||||
sampleRate := recData.SampleRate
|
||||
bitsPerSample := recData.SampleSize
|
||||
|
||||
fmt.Printf("Channels: %v, sampleRate: %v, bitsPerSample: %v\n", channels, sampleRate, bitsPerSample)
|
||||
|
||||
samples, err := wav.WavBytesToSamples(decodedAudioData)
|
||||
if err != nil {
|
||||
err := xerrors.New(err)
|
||||
logger.ErrorContext(ctx, "failed to convert decodedData to samples.", slog.Any("error", err))
|
||||
}
|
||||
|
||||
// Save recording
|
||||
now := time.Now()
|
||||
fileName := fmt.Sprintf("%04d_%02d_%02d_%02d_%02d_%02d.wav",
|
||||
now.Second(), now.Minute(), now.Hour(),
|
||||
now.Day(), now.Month(), now.Year(),
|
||||
)
|
||||
|
||||
err = wav.WriteWavFile(fileName, decodedAudioData, sampleRate, channels, bitsPerSample)
|
||||
if err != nil {
|
||||
err := xerrors.New(err)
|
||||
logger.ErrorContext(ctx, "failed to write wav file.", slog.Any("error", err))
|
||||
}
|
||||
|
||||
/*
|
||||
wav.FFmpegConvertWAV(fileName, fileName, 44100, true)
|
||||
wavInfo, _ := wav.ReadWavInfo("mono_" + fileName)
|
||||
samples, _ = wav.WavBytesToSamples(wavInfo.Data)
|
||||
// spotify.DeleteFile(fileName)
|
||||
spotify.DeleteFile("mono_" + fileName)
|
||||
|
||||
*/
|
||||
|
||||
matches, _, err := shazam.FindMatches(samples, recData.Duration, sampleRate)
|
||||
matches, _, err := shazam.FindMatches(samples, recData.Duration, recData.SampleRate)
|
||||
if err != nil {
|
||||
err := xerrors.New(err)
|
||||
logger.ErrorContext(ctx, "failed to get matches.", slog.Any("error", err))
|
||||
|
|
|
|||
|
|
@ -1,10 +1,37 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"song-recognition/models"
|
||||
"song-recognition/wav"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mdobak/go-xerrors"
|
||||
)
|
||||
|
||||
func DeleteFile(filePath string) error {
|
||||
if _, err := os.Stat(filePath); err == nil {
|
||||
if err := os.RemoveAll(filePath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateFolder(folderPath string) error {
|
||||
err := os.MkdirAll(folderPath, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) {
|
||||
var byteData []byte
|
||||
|
||||
|
|
@ -45,3 +72,52 @@ func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) {
|
|||
|
||||
return byteData, nil
|
||||
}
|
||||
|
||||
func ProcessRecording(recData *models.RecordData, saveRecording bool) ([]float64, error) {
|
||||
decodedAudioData, err := base64.StdEncoding.DecodeString(recData.Audio)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
fileName := fmt.Sprintf("%04d_%02d_%02d_%02d_%02d_%02d.wav",
|
||||
now.Second(), now.Minute(), now.Hour(),
|
||||
now.Day(), now.Month(), now.Year(),
|
||||
)
|
||||
filePath := "tmp/" + fileName
|
||||
|
||||
err = wav.WriteWavFile(filePath, decodedAudioData, recData.SampleRate, recData.Channels, recData.SampleSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reformatedWavFile, err := wav.ReformatWAV(filePath, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
wavInfo, _ := wav.ReadWavInfo(reformatedWavFile)
|
||||
samples, _ := wav.WavBytesToSamples(wavInfo.Data)
|
||||
|
||||
if saveRecording {
|
||||
logger := GetLogger()
|
||||
ctx := context.Background()
|
||||
|
||||
err := CreateFolder("recordings")
|
||||
if err != nil {
|
||||
err := xerrors.New(err)
|
||||
logger.ErrorContext(ctx, "Failed create folder.", slog.Any("error", err))
|
||||
}
|
||||
|
||||
newFilePath := strings.Replace(reformatedWavFile, "tmp/", "recordings/", 1)
|
||||
err = os.Rename(reformatedWavFile, newFilePath)
|
||||
if err != nil {
|
||||
logger.ErrorContext(ctx, "Failed to move file.", slog.Any("error", err))
|
||||
}
|
||||
}
|
||||
|
||||
DeleteFile(fileName)
|
||||
DeleteFile(reformatedWavFile)
|
||||
|
||||
return samples, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue