diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-07-13 17:36:19 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-07-13 17:36:19 (GMT) |
commit | 6162042d3c40a7b9e49531e85f288014d56332b0 (patch) | |
tree | 6c5bebe8a4046da59d0b8f25325bc2e1e38cbff4 /rhimport | |
parent | 54ce457a8e5d7cd41e2944348250c4399bc5fdf0 (diff) |
implemented bs1770 converter
Diffstat (limited to 'rhimport')
-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) // |