|
| 1 | +// Copyright 2024 The Zimtohrli Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// sebass_db creates a study from https://www.audiolabs-erlangen.de/resources/2019-WASPAA-SEBASS/. |
| 16 | +// |
| 17 | +// It currently supports SASSEC, SiSEC08, SAOC, and PEASS-DB. SiSEC18 at |
| 18 | +// that web site doesn't contain all audio, and is not currently supported. |
| 19 | +// |
| 20 | +// Download and unpac one of the supported ZIP files, and use the directory |
| 21 | +// it unpacked as -source when running this binary. |
| 22 | +package main |
| 23 | + |
| 24 | +import ( |
| 25 | + "encoding/csv" |
| 26 | + "flag" |
| 27 | + "fmt" |
| 28 | + "log" |
| 29 | + "math" |
| 30 | + "os" |
| 31 | + "path/filepath" |
| 32 | + "reflect" |
| 33 | + "runtime" |
| 34 | + "strconv" |
| 35 | + |
| 36 | + "github.com/google/zimtohrli/go/aio" |
| 37 | + "github.com/google/zimtohrli/go/data" |
| 38 | + "github.com/google/zimtohrli/go/progress" |
| 39 | + "github.com/google/zimtohrli/go/worker" |
| 40 | +) |
| 41 | + |
| 42 | +func populate(source string, dest string, workers int, failFast bool) error { |
| 43 | + study, err := data.OpenStudy(dest) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + defer study.Close() |
| 48 | + |
| 49 | + csvFiles, err := filepath.Glob(filepath.Join(source, "*.csv")) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + for _, csvFile := range csvFiles { |
| 54 | + signals := "Signals" |
| 55 | + switch filepath.Base(csvFile) { |
| 56 | + case "SAOC_1_anonymized.csv": |
| 57 | + signals = "Signals_1" |
| 58 | + case "SAOC_2_anonymized.csv": |
| 59 | + signals = "Signals_2" |
| 60 | + case "SAOC_3_anonymized.csv": |
| 61 | + signals = "Signals_3" |
| 62 | + } |
| 63 | + fileReader, err := os.Open(csvFile) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + defer fileReader.Close() |
| 68 | + csvReader := csv.NewReader(fileReader) |
| 69 | + header, err := csvReader.Read() |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + if !reflect.DeepEqual(header, []string{"Testname", "Listener", "Trial", "Condition", "Ratingscore"}) { |
| 74 | + return fmt.Errorf("header %+v doesn't match expected SEBASS-DB header", header) |
| 75 | + } |
| 76 | + err = nil |
| 77 | + bar := progress.New(fmt.Sprintf("Transcoding from %q", csvFile)) |
| 78 | + pool := worker.Pool[*data.Reference]{ |
| 79 | + Workers: workers, |
| 80 | + OnChange: bar.Update, |
| 81 | + FailFast: failFast, |
| 82 | + } |
| 83 | + var loopLine []string |
| 84 | + lineIndex := 0 |
| 85 | + for loopLine, err = csvReader.Read(); err == nil; loopLine, err = csvReader.Read() { |
| 86 | + line := loopLine |
| 87 | + if len(line) == 0 { |
| 88 | + continue |
| 89 | + } |
| 90 | + if line[3] == "anchor" { |
| 91 | + line[3] = "anker_mix" |
| 92 | + } |
| 93 | + if line[3] == "hidden_ref" { |
| 94 | + line[3] = "orig" |
| 95 | + } |
| 96 | + if line[3] == "SAOC" { |
| 97 | + continue |
| 98 | + } |
| 99 | + mos, err := strconv.ParseFloat(line[4], 64) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + if math.IsNaN(mos) { |
| 104 | + continue |
| 105 | + } |
| 106 | + refIndex := lineIndex |
| 107 | + pool.Submit(func(f func(*data.Reference)) error { |
| 108 | + ref := &data.Reference{ |
| 109 | + Name: fmt.Sprintf("ref-%v", refIndex), |
| 110 | + } |
| 111 | + var err error |
| 112 | + path := filepath.Join(source, signals, "orig", fmt.Sprintf("%s.wav", line[2])) |
| 113 | + ref.Path, err = aio.Recode(path, dest) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("unable to fetch %q", path) |
| 116 | + } |
| 117 | + dist := &data.Distortion{ |
| 118 | + Name: fmt.Sprintf("dist-%v", refIndex), |
| 119 | + Scores: map[data.ScoreType]float64{ |
| 120 | + data.MOS: mos, |
| 121 | + }, |
| 122 | + } |
| 123 | + path = filepath.Join(source, signals, line[3], fmt.Sprintf("%s.wav", line[2])) |
| 124 | + dist.Path, err = aio.Recode(path, dest) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("unable to fetch %q", path) |
| 127 | + } |
| 128 | + ref.Distortions = append(ref.Distortions, dist) |
| 129 | + f(ref) |
| 130 | + return nil |
| 131 | + }) |
| 132 | + lineIndex++ |
| 133 | + } |
| 134 | + if err := pool.Error(); err != nil { |
| 135 | + log.Println(err.Error()) |
| 136 | + } |
| 137 | + bar.Finish() |
| 138 | + refs := []*data.Reference{} |
| 139 | + for ref := range pool.Results() { |
| 140 | + refs = append(refs, ref) |
| 141 | + } |
| 142 | + if err := study.Put(refs); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + } |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func main() { |
| 150 | + source := flag.String("source", "", "Directory containing one of the unpacked datasets from https://www.audiolabs-erlangen.de/resources/2019-WASPAA-SEBASS/.") |
| 151 | + destination := flag.String("dest", "", "Destination directory.") |
| 152 | + workers := flag.Int("workers", runtime.NumCPU(), "Number of workers transcoding sounds.") |
| 153 | + failFast := flag.Bool("fail_fast", false, "Whether to exit immediately at the first error.") |
| 154 | + flag.Parse() |
| 155 | + if *source == "" || *destination == "" { |
| 156 | + flag.Usage() |
| 157 | + os.Exit(1) |
| 158 | + } |
| 159 | + |
| 160 | + if err := populate(*source, *destination, *workers, *failFast); err != nil { |
| 161 | + log.Fatal(err) |
| 162 | + } |
| 163 | +} |
0 commit comments