diff options
-rw-r--r-- | rhimport/converter.go | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/rhimport/converter.go b/rhimport/converter.go index c809c3c..f7bcb0a 100644 --- a/rhimport/converter.go +++ b/rhimport/converter.go @@ -52,6 +52,8 @@ func NewFetchConverter(convType, filename string, metadata map[string]string) (F return NewNullFetchConverter(filename, metadata) case "ffmpeg": return NewFFMpegFetchConverter(filename, metadata) + case "bs1770": + return NewBS1770FetchConverter(filename, metadata) case "ffmpeg-bs1770": return NewFFMpegBS1770FetchConverter(filename, metadata) } @@ -137,7 +139,75 @@ func (ff *FFMpegFetchConverter) GetResult() (result string, err error, loudnessC } // -// FFMpeg Converter: converts all files into flac and calculates loudness correction value +// FFMpeg/BS1770 Converter: converts all files into flac and calculates loudness correction value +// using ITU BS1770 (EBU R128) +// + +type BS1770FetchConverter struct { + cmd *exec.Cmd + file *os.File + pipe io.WriteCloser + multi io.Writer + result chan ConverterResult +} + +func NewBS1770FetchConverter(filename string, metadata map[string]string) (bs *BS1770FetchConverter, newFilename string, err error) { + bs = &BS1770FetchConverter{} + rhl.Printf("bs1770-converter: opening file '%s'", filename) + newFilename = filename + bs.file, err = os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) + + bs.cmd = exec.Command("bs1770gain", "--ebu", "-i", "--xml", "-") + if bs.pipe, err = bs.cmd.StdinPipe(); err != nil { + return nil, "", err + } + bs.multi = io.MultiWriter(bs.file, bs.pipe) + + var bsStdout, bsStderr bytes.Buffer + bs.cmd.Stdout = &bsStdout + bs.cmd.Stderr = &bsStderr + + bs.result = make(chan ConverterResult, 1) + go func() { + if err := bs.cmd.Run(); err != nil { + bs.result <- ConverterResult{strings.TrimSpace(string(bsStderr.String())), err, 0.0} + } + + res, err := NewBS1770ResultFromXML(&bsStdout) + if err != nil { + bs.result <- ConverterResult{bsStdout.String(), err, 0.0} + } + if len(res.Album.Tracks) == 0 { + bs.result <- ConverterResult{bsStdout.String(), fmt.Errorf("bs1770gain returned no/invalid result"), 0.0} + } + bs.result <- ConverterResult{"", nil, res.Album.Tracks[0].Integrated.LU} + }() + return +} + +func (bs *BS1770FetchConverter) Write(p []byte) (n int, err error) { + return bs.multi.Write(p) +} + +func (bs *BS1770FetchConverter) Close() (err error) { + errPipe := bs.pipe.Close() + errFile := bs.file.Close() + if errFile != nil { + return errFile + } + return errPipe +} + +func (bs *BS1770FetchConverter) GetResult() (result string, err error, loudnessCorr float64) { + if bs.result != nil { + r := <-bs.result + return r.output, r.err, r.loudnessCorr + } + return "", nil, 0.0 +} + +// +// FFMpeg/BS1770 Converter: converts all files into flac and calculates loudness correction value // using ITU BS1770 (EBU R128) // |