mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-17 17:04:22 +00:00
Merge branch 'main' into fix/no-title
This commit is contained in:
commit
603f6eac51
6 changed files with 86 additions and 62 deletions
|
|
@ -315,7 +315,7 @@ func saveSong(filePath string, force bool) error {
|
||||||
wavFile := fileName + ".wav"
|
wavFile := fileName + ".wav"
|
||||||
sourcePath := filepath.Join(filepath.Dir(filePath), wavFile)
|
sourcePath := filepath.Join(filepath.Dir(filePath), wavFile)
|
||||||
newFilePath := filepath.Join(SONGS_DIR, wavFile)
|
newFilePath := filepath.Join(SONGS_DIR, wavFile)
|
||||||
err = os.Rename(sourcePath, newFilePath)
|
err = utils.MoveFile(sourcePath, newFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to rename temporary file to output file: %v", err)
|
return fmt.Errorf("failed to rename temporary file to output file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ func createTables(db *sql.DB) error {
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
artist TEXT NOT NULL,
|
artist TEXT NOT NULL,
|
||||||
ytID TEXT UNIQUE,
|
ytID TEXT,
|
||||||
key TEXT NOT NULL UNIQUE
|
key TEXT NOT NULL UNIQUE
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"song-recognition/shazam"
|
"song-recognition/shazam"
|
||||||
"song-recognition/spotify"
|
"song-recognition/spotify"
|
||||||
"song-recognition/utils"
|
"song-recognition/utils"
|
||||||
|
"song-recognition/wav"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
socketio "github.com/googollee/go-socket.io"
|
socketio "github.com/googollee/go-socket.io"
|
||||||
|
|
@ -186,7 +187,7 @@ func handleNewRecording(socket socketio.Conn, recordData string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
samples, err := utils.ProcessRecording(&recData, true)
|
samples, err := wav.ProcessRecording(&recData, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := xerrors.New(err)
|
err := xerrors.New(err)
|
||||||
logger.ErrorContext(ctx, "Failed to process recording.", slog.Any("error", err))
|
logger.ErrorContext(ctx, "Failed to process recording.", slog.Any("error", err))
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,10 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"song-recognition/models"
|
|
||||||
"song-recognition/wav"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mdobak/go-xerrors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeleteFile(filePath string) error {
|
func DeleteFile(filePath string) error {
|
||||||
|
|
@ -32,6 +24,36 @@ func CreateFolder(folderPath string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MoveFile(sourcePath string, destinationPath string) error {
|
||||||
|
srcFile, err := os.Open(sourcePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
destFile, err := os.Create(destinationPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer destFile.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(destFile, srcFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = srcFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.Remove(sourcePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) {
|
func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) {
|
||||||
var byteData []byte
|
var byteData []byte
|
||||||
|
|
||||||
|
|
@ -72,52 +94,3 @@ func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) {
|
||||||
|
|
||||||
return byteData, nil
|
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"song-recognition/utils"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -43,7 +44,7 @@ func ConvertToWAV(inputFilePath string, channels int) (wavFilePath string, err e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename the temporary file to the output file
|
// Rename the temporary file to the output file
|
||||||
err = os.Rename(tmpFile, outputFile)
|
err = utils.MoveFile(tmpFile, outputFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to rename temporary file to output file: %v", err)
|
return "", fmt.Errorf("failed to rename temporary file to output file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
49
wav/wav.go
49
wav/wav.go
|
|
@ -209,3 +209,52 @@ func GetMetadata(filePath string) (FFmpegMetadata, error) {
|
||||||
|
|
||||||
return metadata, nil
|
return metadata, 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 = WriteWavFile(filePath, decodedAudioData, recData.SampleRate, recData.Channels, recData.SampleSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
reformatedWavFile, err := ReformatWAV(filePath, 1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
wavInfo, _ := ReadWavInfo(reformatedWavFile)
|
||||||
|
samples, _ := WavBytesToSamples(wavInfo.Data)
|
||||||
|
|
||||||
|
if saveRecording {
|
||||||
|
logger := utils.GetLogger()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
err := utils.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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.DeleteFile(fileName)
|
||||||
|
utils.DeleteFile(reformatedWavFile)
|
||||||
|
|
||||||
|
return samples, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue