diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-07-13 19:40:31 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-07-13 19:40:31 (GMT) |
commit | 3c67fe07b6103d1bd0ddafbfa7da0544cf7ed83e (patch) | |
tree | 6f7a60f03ff6a3a40729e58b3ade74a620ab8232 /rhimport | |
parent | 6162042d3c40a7b9e49531e85f288014d56332b0 (diff) |
added normalizer step
always send last/first progress message of a step
Diffstat (limited to 'rhimport')
-rw-r--r-- | rhimport/converter.go | 43 | ||||
-rw-r--r-- | rhimport/importer.go | 2 | ||||
-rw-r--r-- | rhimport/normalizer.go | 73 | ||||
-rw-r--r-- | rhimport/session.go | 10 |
4 files changed, 105 insertions, 23 deletions
diff --git a/rhimport/converter.go b/rhimport/converter.go index f7bcb0a..dab3309 100644 --- a/rhimport/converter.go +++ b/rhimport/converter.go @@ -40,7 +40,7 @@ type FetchConverter interface { GetResult() (result string, err error, loudnessCorr float64) } -type ConverterResult struct { +type FetchConverterResult struct { output string err error loudnessCorr float64 @@ -95,7 +95,7 @@ func (c *NullFetchConverter) GetResult() (result string, err error, loudnessCorr type FFMpegFetchConverter struct { cmd *exec.Cmd pipe io.WriteCloser - result chan ConverterResult + result chan FetchConverterResult } func NewFFMpegFetchConverter(filename string, metadata map[string]string) (ff *FFMpegFetchConverter, filenameFlac string, err error) { @@ -114,10 +114,10 @@ func NewFFMpegFetchConverter(filename string, metadata map[string]string) (ff *F return nil, "", err } - ff.result = make(chan ConverterResult, 1) + ff.result = make(chan FetchConverterResult, 1) go func() { output, err := ff.cmd.CombinedOutput() - ff.result <- ConverterResult{strings.TrimSpace(string(output)), err, 0.0} + ff.result <- FetchConverterResult{strings.TrimSpace(string(output)), err, 0.0} }() return } @@ -139,8 +139,7 @@ func (ff *FFMpegFetchConverter) GetResult() (result string, err error, loudnessC } // -// FFMpeg/BS1770 Converter: converts all files into flac and calculates loudness correction value -// using ITU BS1770 (EBU R128) +// BS1770 Converter: calculates loudness correction value using ITU BS1770 (EBU R128) // type BS1770FetchConverter struct { @@ -148,7 +147,7 @@ type BS1770FetchConverter struct { file *os.File pipe io.WriteCloser multi io.Writer - result chan ConverterResult + result chan FetchConverterResult } func NewBS1770FetchConverter(filename string, metadata map[string]string) (bs *BS1770FetchConverter, newFilename string, err error) { @@ -167,20 +166,20 @@ func NewBS1770FetchConverter(filename string, metadata map[string]string) (bs *B bs.cmd.Stdout = &bsStdout bs.cmd.Stderr = &bsStderr - bs.result = make(chan ConverterResult, 1) + bs.result = make(chan FetchConverterResult, 1) go func() { if err := bs.cmd.Run(); err != nil { - bs.result <- ConverterResult{strings.TrimSpace(string(bsStderr.String())), err, 0.0} + bs.result <- FetchConverterResult{strings.TrimSpace(string(bsStderr.String())), err, 0.0} } res, err := NewBS1770ResultFromXML(&bsStdout) if err != nil { - bs.result <- ConverterResult{bsStdout.String(), err, 0.0} + bs.result <- FetchConverterResult{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 <- FetchConverterResult{bsStdout.String(), fmt.Errorf("bs1770gain returned no/invalid result"), 0.0} } - bs.result <- ConverterResult{"", nil, res.Album.Tracks[0].Integrated.LU} + bs.result <- FetchConverterResult{"", nil, res.Album.Tracks[0].Integrated.LU} }() return } @@ -215,8 +214,8 @@ type FFMpegBS1770FetchConverter struct { ffmpeg *exec.Cmd bs1770 *exec.Cmd pipe io.WriteCloser - resultFF chan ConverterResult - resultBS chan ConverterResult + resultFF chan FetchConverterResult + resultBS chan FetchConverterResult } func NewFFMpegBS1770FetchConverter(filename string, metadata map[string]string) (ff *FFMpegBS1770FetchConverter, filenameFlac string, err error) { @@ -248,26 +247,26 @@ func NewFFMpegBS1770FetchConverter(filename string, metadata map[string]string) ff.bs1770.Stdout = &bsStdout ff.bs1770.Stderr = &bsStderr - ff.resultFF = make(chan ConverterResult, 1) - ff.resultBS = make(chan ConverterResult, 1) + ff.resultFF = make(chan FetchConverterResult, 1) + ff.resultBS = make(chan FetchConverterResult, 1) go func() { err := ff.ffmpeg.Run() ffstdout.Close() - ff.resultFF <- ConverterResult{strings.TrimSpace(string(ffStderr.String())), err, 0.0} + ff.resultFF <- FetchConverterResult{strings.TrimSpace(string(ffStderr.String())), err, 0.0} }() go func() { if err := ff.bs1770.Run(); err != nil { - ff.resultBS <- ConverterResult{strings.TrimSpace(string(bsStderr.String())), err, 0.0} + ff.resultBS <- FetchConverterResult{strings.TrimSpace(string(bsStderr.String())), err, 0.0} } res, err := NewBS1770ResultFromXML(&bsStdout) if err != nil { - ff.resultBS <- ConverterResult{bsStdout.String(), err, 0.0} + ff.resultBS <- FetchConverterResult{bsStdout.String(), err, 0.0} } if len(res.Album.Tracks) == 0 { - ff.resultBS <- ConverterResult{bsStdout.String(), fmt.Errorf("bs1770gain returned no/invalid result"), 0.0} + ff.resultBS <- FetchConverterResult{bsStdout.String(), fmt.Errorf("bs1770gain returned no/invalid result"), 0.0} } - ff.resultBS <- ConverterResult{"", nil, res.Album.Tracks[0].Integrated.LU} + ff.resultBS <- FetchConverterResult{"", nil, res.Album.Tracks[0].Integrated.LU} }() return } @@ -281,7 +280,7 @@ func (ff *FFMpegBS1770FetchConverter) Close() (err error) { } func (ff *FFMpegBS1770FetchConverter) GetResult() (result string, err error, loudnessCorr float64) { - var rff, rbs ConverterResult + var rff, rbs FetchConverterResult if ff.resultFF != nil { rff = <-ff.resultFF } diff --git a/rhimport/importer.go b/rhimport/importer.go index e7eca56..518b242 100644 --- a/rhimport/importer.go +++ b/rhimport/importer.go @@ -312,7 +312,7 @@ func importAudio(ctx *Context, res *Result) (err error) { } if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(2, "importing", ulnow, ultotal, ctx.ProgressCallBackData); !keep { + if keep := ctx.ProgressCallBack(3, "importing", ulnow, ultotal, ctx.ProgressCallBackData); !keep { ctx.ProgressCallBack = nil } } diff --git a/rhimport/normalizer.go b/rhimport/normalizer.go new file mode 100644 index 0000000..3730929 --- /dev/null +++ b/rhimport/normalizer.go @@ -0,0 +1,73 @@ +// +// rhimportd +// +// The Radio Helsinki Rivendell Import Daemon +// +// +// Copyright (C) 2015-2016 Christian Pointner <equinox@helsinki.at> +// +// This file is part of rhimportd. +// +// rhimportd is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// rhimportd is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with rhimportd. If not, see <http://www.gnu.org/licenses/>. +// + +package rhimport + +import ( + "os" +) + +func NormalizeFile(ctx *Context) (err error) { + if ctx.LoudnessCorr == 0.0 { + rhdl.Println("NormalizeFile: skipping normalization since the gain = 0.0dB") + if ctx.ProgressCallBack != nil { + if keep := ctx.ProgressCallBack(2, "normalizing", 1.0, 1.0, ctx.ProgressCallBackData); !keep { + ctx.ProgressCallBack = nil + } + } + return + } + + rhl.Printf("NormalizeFile: using gain = %.2fdB", ctx.LoudnessCorr) + ctx.NormalizationLevel = 0 // don't do double normalization + + var src *os.File + if src, err = os.Open(ctx.SourceFile); err != nil { + return + } + defer src.Close() + + size := int64(0) + if info, err := src.Stat(); err != nil { + return err + } else { + size = info.Size() + } + + if ctx.ProgressCallBack != nil { + if keep := ctx.ProgressCallBack(2, "normalizing", 0.0, float64(size), ctx.ProgressCallBackData); !keep { + ctx.ProgressCallBack = nil + } + } + + // TODO: call ffmpeg + + if ctx.ProgressCallBack != nil { + if keep := ctx.ProgressCallBack(2, "normalizing", float64(size), float64(size), ctx.ProgressCallBackData); !keep { + ctx.ProgressCallBack = nil + } + } + + return +} diff --git a/rhimport/session.go b/rhimport/session.go index b00be17..b0031b1 100644 --- a/rhimport/session.go +++ b/rhimport/session.go @@ -118,6 +118,12 @@ func sessionRun(ctx Context, done chan<- Result) { return } + err = NormalizeFile(&ctx) + if err != nil { + done <- Result{ResponseCode: http.StatusInternalServerError, ErrorString: err.Error()} + return + } + res, err = ImportFile(&ctx) if err != nil { res.ResponseCode = http.StatusInternalServerError @@ -233,6 +239,10 @@ func (self *Session) dispatchRequests() { if lastProgress == nil { self.callProgressHandler(&p) pt.Reset(self.progressRateLimit) + } else if lastProgress.Step != p.Step { + self.callProgressHandler(lastProgress) + self.callProgressHandler(&p) + pt.Reset(self.progressRateLimit) } lastProgress = &p progressPending++ |