You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.5 KiB
Go

4 weeks ago
package icsutil
import (
"batchmodule/icsconf"
"batchmodule/icserror"
"batchmodule/icslog"
"encoding/binary"
"fmt"
"io"
"log"
"os"
)
var (
conf icsconf.AppInfo
l *log.Logger
)
func init() {
conf = icsconf.Getconfig()
l = icslog.InitializeLogger()
}
func DeletePcmFolder(dir string) (bool, *icserror.IcsError) {
l.Println("util.go>deletePcmFolder> dir: ", dir)
if _, err := os.Stat(dir); os.IsNotExist(err) {
l.Println("Error at util.go>deletePcmFolder> pcm folder does not exist with the dir: ", dir)
return false, icserror.ICSERRFileOpen
}
err := os.RemoveAll(dir)
if err != nil {
l.Panicln("Error at util.go>deletePcmFolder> os.RemoveAll() dir: ", dir)
return false, icserror.ICSERRWriteFile
}
l.Printf(`util.go>deletePcmFolder> %s has been successfully deleted`, dir)
return true, nil
}
func DevideWavTo2Pcm(fileName string, starttime string) (result bool, folderName string) {
pcmDir := conf.Directories.PCMDirectory
starttimeDir := fmt.Sprintf("%s/%s", pcmDir, starttime)
os.MkdirAll(starttimeDir, os.ModePerm)
leftOutputFile := fmt.Sprintf(`%s/%s-left.pcm`, starttimeDir, starttime)
rightOutputFile := fmt.Sprintf(`%s/%s-right.pcm`, starttimeDir, starttime)
inFile, err := os.Open(fileName)
if err != nil {
l.Printf("Error at util.go>devideWavTo2Pcm()> os.Open failed. dir: %s", fileName)
return false, ""
}
defer inFile.Close()
leftFile, err := os.Create(leftOutputFile)
if err != nil {
l.Printf("Error at util.go>devideWavTo2Pcm()> os.Create failed. dir: %s", leftOutputFile)
return false, ""
}
defer leftFile.Close()
rightFile, err := os.Create(rightOutputFile)
if err != nil {
l.Printf("Error at util.go>devideWavTo2Pcm()> os.Create failed. dir: %s", leftOutputFile)
return false, ""
}
defer rightFile.Close()
// Skip WAV header (44 bytes)
wavHeader := make([]byte, 44)
if _, err := inFile.Read(wavHeader); err != nil {
l.Printf("Error at util.go>devideWavTo2Pcm()> inFile.Read(wavHeader) failed. err: %+v", err)
return false, ""
}
// Check if it's a valid WAV file
if string(wavHeader[:4]) != "RIFF" || string(wavHeader[8:12]) != "WAVE" {
l.Printf("Error at util.go>devideWavTo2Pcm()> invalid WAV file format. Header content: %s", string(wavHeader[:4]))
return false, ""
}
// Check if WAV file is stereo
numChannels := binary.LittleEndian.Uint16(wavHeader[22:24])
if numChannels != 2 {
l.Printf("Error at util.go>devideWavTo2Pcm()> unsupported channel count: %d. This function only supports stereo (2 channels)", numChannels)
return false, ""
}
buf := make([]byte, 4) // 2 bytes per sample * 2 channels
for {
n, err := inFile.Read(buf)
if err == io.EOF {
break
}
if err != nil {
l.Printf("Error at util.go>devideWavTo2Pcm()> inFile.Read(buf) failed. failed to read from input file: %v", err)
return false, ""
}
if n < 4 {
for i := n; i < 4; i++ {
buf[i] = 0
}
break
}
leftSample := buf[:2]
rightSample := buf[2:]
if err := binary.Write(leftFile, binary.LittleEndian, leftSample); err != nil {
l.Printf("Error at util.go>devideWavTo2Pcm()> binary.Write(leftFile, binary.LittleEndian, leftSample) failed. err: %v", err)
return false, ""
}
if err := binary.Write(rightFile, binary.LittleEndian, rightSample); err != nil {
l.Printf("Error at util.go>devideWavTo2Pcm()> binary.Write(rightFile, binary.LittleEndian, rightSample) failed. err: %v", err)
return false, ""
}
}
l.Printf("util.go>devideWavTo2Pcm()> WAV file split successfully to dir: %s, %s", leftOutputFile, rightOutputFile)
return true, starttime
}