From 07b1b44dd3919691c4dcdfc2f0da552912a4b51e Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 15 Jul 2016 21:08:31 +0200 Subject: improved sanity checking before fetch/import starts fix bug which lead to double normalization diff --git a/rhimport/conf.go b/rhimport/conf.go index 942bb62..502882d 100644 --- a/rhimport/conf.go +++ b/rhimport/conf.go @@ -24,6 +24,12 @@ package rhimport +import ( + "strconv" + + "github.com/vaughan0/go-ini" +) + type ImportParamDefaults struct { Channels uint NormalizationLevel int @@ -32,20 +38,59 @@ type ImportParamDefaults struct { } type Config struct { + ConfigFile string RDXportEndpoint string TempDir string LocalFetchDir string + SampleRate uint ImportParamDefaults } -func NewConfig(rdxportEndpoint, tempDir, localFetchDir string) (conf *Config) { +func getIniValue(file ini.File, section string, key string, dflt string) string { + value, ok := file.Get(section, key) + if ok { + return value + } + return dflt +} + +func (c *Config) readConfigFile() error { + file, err := ini.LoadFile(c.ConfigFile) + if err != nil { + return err + } + + sr := getIniValue(file, "Format", "SampleRate", "44100") + if sr64, err := strconv.ParseUint(sr, 10, 32); err != nil { + return err + } else { + c.SampleRate = uint(sr64) + } + + ch := getIniValue(file, "Format", "Channels", "2") + if ch64, err := strconv.ParseUint(ch, 10, 32); err != nil { + return err + } else { + c.ImportParamDefaults.Channels = uint(ch64) + } + + return nil +} + +func NewConfig(configfile, rdxportEndpoint, tempDir, localFetchDir string) (conf *Config, err error) { conf = new(Config) + conf.ConfigFile = configfile conf.RDXportEndpoint = rdxportEndpoint conf.TempDir = tempDir conf.LocalFetchDir = localFetchDir + conf.SampleRate = 44100 conf.ImportParamDefaults.Channels = 2 conf.ImportParamDefaults.NormalizationLevel = 0 conf.ImportParamDefaults.AutotrimLevel = 0 conf.ImportParamDefaults.UseMetaData = true + + if err = conf.readConfigFile(); err != nil { + return + } return } diff --git a/rhimport/converter.go b/rhimport/converter.go index a116a8d..72f746d 100644 --- a/rhimport/converter.go +++ b/rhimport/converter.go @@ -48,18 +48,22 @@ type FetchConverterResult struct { loudnessCorr float64 } -func NewFetchConverter(convType, filename string, metadata map[string]string, channels uint) (FetchConverter, string, error) { - switch convType { +func NewFetchConverter(ctx *Context, filename string, metadata map[string]string) (FetchConverter, string, error) { + switch ctx.FetchConverter { case "null": - return NewNullFetchConverter(filename, metadata, channels) + // no support for loudness evaluation - leave normalization to Rivendell + return NewNullFetchConverter(filename, metadata, ctx.conf.SampleRate, ctx.Channels) case "ffmpeg": - return NewFFMpegFetchConverter(filename, metadata, channels) + // no support for loudness evaluation - leave normalization to Rivendell + return NewFFMpegFetchConverter(filename, metadata, ctx.conf.SampleRate, ctx.Channels) case "bs1770": - return NewBS1770FetchConverter(filename, metadata, channels) + ctx.NormalizationLevel = 0 // disable Rivendell normalization + return NewBS1770FetchConverter(filename, metadata, ctx.conf.SampleRate, ctx.Channels) case "ffmpeg-bs1770": - return NewFFMpegBS1770FetchConverter(filename, metadata, channels) + ctx.NormalizationLevel = 0 // disable Rivendell normalization + return NewFFMpegBS1770FetchConverter(filename, metadata, ctx.conf.SampleRate, ctx.Channels) } - return nil, "", errors.New("unknown fetch converter type: " + convType) + return nil, "", errors.New("unknown fetch converter type: " + ctx.FetchConverter) } // @@ -70,7 +74,7 @@ type NullFetchConverter struct { file *os.File } -func NewNullFetchConverter(filename string, metadata map[string]string, channels uint) (n *NullFetchConverter, newFilename string, err error) { +func NewNullFetchConverter(filename string, metadata map[string]string, samplerate, channels uint) (n *NullFetchConverter, newFilename string, err error) { n = &NullFetchConverter{} rhl.Printf("null-converter: opening file '%s'", filename) newFilename = filename @@ -100,7 +104,7 @@ type FFMpegFetchConverter struct { result chan FetchConverterResult } -func NewFFMpegFetchConverter(filename string, metadata map[string]string, channels uint) (ff *FFMpegFetchConverter, filenameFlac string, err error) { +func NewFFMpegFetchConverter(filename string, metadata map[string]string, samplerate, channels uint) (ff *FFMpegFetchConverter, filenameFlac string, err error) { ff = &FFMpegFetchConverter{} ext := filepath.Ext(filename) filenameFlac = strings.TrimSuffix(filename, ext) + ".flac" @@ -111,7 +115,7 @@ func NewFFMpegFetchConverter(filename string, metadata map[string]string, channe ff.cmd.Args = append(ff.cmd.Args, "-metadata", fmt.Sprintf("%s=%s", key, value)) } } - ff.cmd.Args = append(ff.cmd.Args, "-ac", strconv.FormatUint(uint64(channels), 10), "-f", "flac", filenameFlac) + ff.cmd.Args = append(ff.cmd.Args, "-ar", strconv.FormatUint(uint64(samplerate), 10), "-ac", strconv.FormatUint(uint64(channels), 10), "-f", "flac", filenameFlac) if ff.pipe, err = ff.cmd.StdinPipe(); err != nil { return nil, "", err } @@ -160,7 +164,7 @@ type BS1770FetchConverter struct { result chan FetchConverterResult } -func NewBS1770FetchConverter(filename string, metadata map[string]string, channels uint) (bs *BS1770FetchConverter, newFilename string, err error) { +func NewBS1770FetchConverter(filename string, metadata map[string]string, samplerate, channels uint) (bs *BS1770FetchConverter, newFilename string, err error) { bs = &BS1770FetchConverter{} rhl.Printf("bs1770-converter: starting bs1770gain for file '%s'", filename) newFilename = filename @@ -236,7 +240,7 @@ type FFMpegBS1770FetchConverter struct { resultBS chan FetchConverterResult } -func NewFFMpegBS1770FetchConverter(filename string, metadata map[string]string, channels uint) (ff *FFMpegBS1770FetchConverter, filenameFlac string, err error) { +func NewFFMpegBS1770FetchConverter(filename string, metadata map[string]string, samplerate, channels uint) (ff *FFMpegBS1770FetchConverter, filenameFlac string, err error) { ff = &FFMpegBS1770FetchConverter{} ext := filepath.Ext(filename) filenameFlac = strings.TrimSuffix(filename, ext) + ".flac" @@ -247,8 +251,8 @@ func NewFFMpegBS1770FetchConverter(filename string, metadata map[string]string, ff.ffmpeg.Args = append(ff.ffmpeg.Args, "-metadata", fmt.Sprintf("%s=%s", key, value)) } } - ff.ffmpeg.Args = append(ff.ffmpeg.Args, "-ac", strconv.FormatUint(uint64(channels), 10), "-f", "flac", filenameFlac) - ff.ffmpeg.Args = append(ff.ffmpeg.Args, "-ac", strconv.FormatUint(uint64(channels), 10), "-f", "flac", "pipe:1") + ff.ffmpeg.Args = append(ff.ffmpeg.Args, "-ar", strconv.FormatUint(uint64(samplerate), 10), "-ac", strconv.FormatUint(uint64(channels), 10), "-f", "flac", filenameFlac) + ff.ffmpeg.Args = append(ff.ffmpeg.Args, "-ar", strconv.FormatUint(uint64(samplerate), 10), "-ac", strconv.FormatUint(uint64(channels), 10), "-f", "flac", "pipe:1") if ff.pipe, err = ff.ffmpeg.StdinPipe(); err != nil { return nil, "", err } diff --git a/rhimport/core.go b/rhimport/core.go index fc92f6a..67d46a4 100644 --- a/rhimport/core.go +++ b/rhimport/core.go @@ -119,6 +119,7 @@ type Context struct { Trusted bool ShowId uint ClearShowCarts bool + ShowCarts []uint GroupName string Cart uint ClearCart bool @@ -185,10 +186,21 @@ func (ctx *Context) SanityCheck() error { if ctx.ShowId != 0 && ctx.ShowId > CART_MAX { return fmt.Errorf("ShowId %d is outside of allowed range (0 < show-id < %d)", ctx.ShowId, CART_MAX) } - if ctx.Cart != 0 && ctx.Cart > CART_MAX { + if err := ctx.getShowInfo(); err != nil { + return err + } + if ctx.Cart == 0 { + return nil + } + if ctx.Cart > CART_MAX { return fmt.Errorf("Cart %d is outside of allowed range (0 < cart < %d)", ctx.Cart, CART_MAX) } - return nil + for _, cart := range ctx.ShowCarts { + if cart == ctx.Cart { + return nil + } + } + return fmt.Errorf("Cart %d is not part of the show with show-id %d", ctx.Cart, ctx.ShowId) } if ctx.GroupName != "" { ismusic, err := ctx.checkMusicGroup() @@ -201,7 +213,7 @@ func (ctx *Context) SanityCheck() error { if ctx.Cart != 0 || ctx.Cut != 0 { return fmt.Errorf("Cart and Cut must not be supplied when importing into a music group") } - return nil + return ctx.getMusicInfo() } if ctx.Cart == 0 { return fmt.Errorf("either ShowId, PoolName or CartNumber must be supplied") @@ -232,8 +244,8 @@ func (ctx *Context) getGroupOfCart() (err error) { return } -func (ctx *Context) getShowInfo() (carts []uint, err error) { - ctx.GroupName, ctx.NormalizationLevel, ctx.AutotrimLevel, carts, err = ctx.db.GetShowInfo(ctx.ShowId) +func (ctx *Context) getShowInfo() (err error) { + ctx.GroupName, ctx.NormalizationLevel, ctx.AutotrimLevel, ctx.ShowCarts, err = ctx.db.GetShowInfo(ctx.ShowId) ctx.Channels = 2 ctx.UseMetaData = true return diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index 45434a6..9170a86 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -83,7 +83,7 @@ func curlWriteCallback(ptr []byte, userdata interface{}) bool { data.filename = filepath.Join(data.basepath, name) } data.ctx.OrigFilename = data.filename - conv, newFilename, err := NewFetchConverter(data.ctx.FetchConverter, data.filename, data.metadata, data.ctx.Channels) + conv, newFilename, err := NewFetchConverter(data.ctx, data.filename, data.metadata) if err != nil { rhl.Printf("Unable to create converter for file %s: %s", data.filename, err) data.writeError = err @@ -210,7 +210,10 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { } } - if info.Extractor != "generic" && info.Extractor != "dropbox" { + switch strings.ToLower(info.Extractor) { + case "generic": + case "dropbox": + default: ctx.Title = info.ExtractorKey + ": " + info.Title if info.Title == "" { ctx.Title += info.ID @@ -425,7 +428,7 @@ func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal ctx.OrigFilename = ctx.SourceFile var conv FetchConverter - if conv, ctx.SourceFile, err = NewFetchConverter(ctx.FetchConverter, filepath.Join(basepath, origFile), nil, ctx.Channels); err != nil { + if conv, ctx.SourceFile, err = NewFetchConverter(ctx, filepath.Join(basepath, origFile), nil); err != nil { rhl.Printf("Unable to create converter for file %s: %s", origDir+origFile, err) return } @@ -645,7 +648,7 @@ func fetchFileAttachment(ctx *Context, res *Result, uri *url.URL) error { var conv FetchConverter ctx.OrigFilename = ctx.SourceFile - if conv, ctx.SourceFile, err = NewFetchConverter(ctx.FetchConverter, ctx.SourceFile, nil, ctx.Channels); err != nil { + if conv, ctx.SourceFile, err = NewFetchConverter(ctx, ctx.SourceFile, nil); err != nil { rhl.Printf("Unable to create converter for file %s: %s", ctx.OrigFilename, err) return err } diff --git a/rhimport/importer.go b/rhimport/importer.go index d7365a5..9b29e97 100644 --- a/rhimport/importer.go +++ b/rhimport/importer.go @@ -359,21 +359,6 @@ func removeAddCartCut(ctx *Context, res *Result) (err error) { return addCartCut(ctx, res) } -func isCartMemberOfShow(ctx *Context, res *Result, carts []uint) (found bool) { - if ctx.Cart == 0 { - return true - } - for _, cart := range carts { - if cart == ctx.Cart { - return true - } - } - res.ResponseCode = http.StatusBadRequest - res.ErrorString = fmt.Sprintf("Requested cart %d is not a member of show: %d", ctx.Cart, ctx.ShowId) - res.Cart = ctx.Cart - return false -} - func clearShowCarts(ctx *Context, res *Result, carts []uint) (err error) { if ctx.ClearShowCarts { origCart := ctx.Cart @@ -453,14 +438,7 @@ func ImportFile(ctx *Context) (res *Result, err error) { rmCartOnErr := false rmCutOnErr := false if ctx.ShowId != 0 { // Import to a show - var showCarts []uint - if showCarts, err = ctx.getShowInfo(); err != nil { - return - } - if !isCartMemberOfShow(ctx, res, showCarts) { - return - } - if err = clearShowCarts(ctx, res, showCarts); err != nil || (res.ResponseCode != http.StatusOK && res.ResponseCode != http.StatusNotFound) { + if err = clearShowCarts(ctx, res, ctx.ShowCarts); err != nil || (res.ResponseCode != http.StatusOK && res.ResponseCode != http.StatusNotFound) { return } if ctx.ClearCart && !ctx.ClearShowCarts { @@ -468,15 +446,12 @@ func ImportFile(ctx *Context) (res *Result, err error) { return } } else { - if err = addShowCartCut(ctx, res, showCarts); err != nil || res.ResponseCode != http.StatusOK { + if err = addShowCartCut(ctx, res, ctx.ShowCarts); err != nil || res.ResponseCode != http.StatusOK { return } } rmCartOnErr = true } else if ctx.GroupName != "" { // Import to music pool - if err = ctx.getMusicInfo(); err != nil { - return - } if err = addCartCut(ctx, res); err != nil || res.ResponseCode != http.StatusOK { return } diff --git a/rhimport/normalizer.go b/rhimport/normalizer.go index 0be9938..840dde2 100644 --- a/rhimport/normalizer.go +++ b/rhimport/normalizer.go @@ -104,8 +104,6 @@ func NormalizeFile(ctx *Context) (res *Result, err error) { return } - ctx.NormalizationLevel = 0 // don't do double normalization - var src *os.File if src, err = os.Open(ctx.SourceFile); err != nil { return -- cgit v0.10.2