2026-01-20 17:10:22 +00:00
|
|
|
package background
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/userfile"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-21 03:30:03 +00:00
|
|
|
// AudioJob represents a job to process an audio file.
|
|
|
|
|
type jobAudio struct {
|
|
|
|
|
AudioUUID uuid.UUID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var channelJobAudio chan jobAudio
|
|
|
|
|
|
|
|
|
|
func AudioTranscode(audio_uuid uuid.UUID) {
|
|
|
|
|
enqueueAudioJob(jobAudio{
|
|
|
|
|
AudioUUID: audio_uuid,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// startAudioWorker initializes the audio job channel and starts the worker goroutine.
|
|
|
|
|
func startWorkerAudio(ctx context.Context, audioJobChannel chan jobAudio) {
|
2026-01-20 17:10:22 +00:00
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Info().Msg("Audio worker shutting down.")
|
|
|
|
|
return
|
|
|
|
|
case job := <-audioJobChannel:
|
|
|
|
|
log.Info().Str("uuid", job.AudioUUID.String()).Msg("Processing audio job")
|
|
|
|
|
err := processAudioFile(job.AudioUUID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Str("uuid", job.AudioUUID.String()).Msg("Error processing audio file")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 03:30:03 +00:00
|
|
|
// EnqueueAudioJob sends an audio processing job to the worker.
|
|
|
|
|
func enqueueAudioJob(job jobAudio) {
|
|
|
|
|
select {
|
|
|
|
|
case channelJobAudio <- job:
|
|
|
|
|
log.Info().Str("uuid", job.AudioUUID.String()).Msg("Enqueued audio job")
|
|
|
|
|
default:
|
|
|
|
|
log.Warn().Str("uuid", job.AudioUUID.String()).Msg("Audio job channel is full, dropping job")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 17:10:22 +00:00
|
|
|
func processAudioFile(audioUUID uuid.UUID) error {
|
|
|
|
|
// Normalize audio
|
2026-02-08 00:58:51 +00:00
|
|
|
err := userfile.NormalizeAudio(audioUUID)
|
2026-01-20 17:10:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to normalize audio %s: %v", audioUUID, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Transcode to OGG
|
2026-02-08 00:58:51 +00:00
|
|
|
err = userfile.TranscodeToOgg(audioUUID)
|
2026-01-20 17:10:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to transcode audio %s to OGG: %v", audioUUID, err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 03:30:03 +00:00
|
|
|
enqueueLabelStudioJob(jobLabelStudio{
|
2026-01-20 17:10:22 +00:00
|
|
|
UUID: audioUUID,
|
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|