Skip to content

Commit

Permalink
seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Aug 5, 2021
1 parent cefcc32 commit 22d75f6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
26 changes: 20 additions & 6 deletions src/audiosegment/audiosegment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ func init() {

const SECONDSATEND = 0.1

func SplitEqual(fname string, secondsMax float64, secondsOverlap float64) (allSegments [][]models.AudioSegment, err error) {
func SplitEqual(fname string, secondsMax float64, secondsOverlap float64, splices int) (allSegments [][]models.AudioSegment, err error) {
err = Convert(fname, fname+".mp3")
if err != nil {
return
}
fname = fname + ".mp3"

if splices > 0 {
secondsOverlap = 0
}

cmd := fmt.Sprintf("%s", fname)
logger.Debug(cmd)
out, err := exec.Command("ffprobe", strings.Fields(cmd)...).CombinedOutput()
Expand Down Expand Up @@ -84,11 +88,21 @@ func SplitEqual(fname string, secondsMax float64, secondsOverlap float64) (allSe
continue
}

r.segments, r.err = ffmpeg.SplitOnSilence(fnameTrunc, -22, 0.2, -0.2)
if r.err != nil {
logger.Error(r.err)
results <- r
continue
if splices == 0 {
r.segments, r.err = ffmpeg.SplitOnSilence(fnameTrunc, -22, 0.2, -0.2)
if r.err != nil {
logger.Error(r.err)
results <- r
continue
}
} else {
r.segments = make([]models.AudioSegment, splices)
for i, _ := range r.segments {
r.segments[i].Start = secondsDuration * float64(i) / float64(splices)
r.segments[i].End = secondsDuration * float64(i+1) / float64(splices)
r.segments[i].Duration = r.segments[i].End - r.segments[i].Start
r.segments[i].Filename = fnameTrunc
}
}
r.err = DrawSegments(r.segments)
if r.err != nil {
Expand Down
13 changes: 9 additions & 4 deletions src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type Metadata struct {
IsSynthPatch bool
RemoveSilence bool
RootNote string
Splices int
}

type FileData struct {
Expand Down Expand Up @@ -336,6 +337,7 @@ func viewPatch(w http.ResponseWriter, r *http.Request) (err error) {
patchtypeA, _ := r.URL.Query()["synthPatch"]
removeSilenceA, _ := r.URL.Query()["removeSilence"]
rootNoteA, _ := r.URL.Query()["rootNote"]
splicesA, _ := r.URL.Query()["splices"]
patchtype := "drum"
removeSilence := false
rootNote := "A"
Expand Down Expand Up @@ -364,8 +366,10 @@ func viewPatch(w http.ResponseWriter, r *http.Request) (err error) {
if secondsEnd[0] != "" {
startStop[1], _ = strconv.ParseFloat(secondsEnd[0], 64)
}
splices, _ := strconv.Atoi(splicesA[0])
log.Debugf("splices: %d", splices)

uuid, err := generateUserData(audioURL[0], startStop, patchtype, removeSilence, rootNote)
uuid, err := generateUserData(audioURL[0], startStop, patchtype, removeSilence, rootNote, splices)
if err != nil {
return
}
Expand Down Expand Up @@ -395,7 +399,7 @@ func viewMain(w http.ResponseWriter, r *http.Request, messageError string, templ
return
}

func generateUserData(u string, startStop []float64, patchType string, removeSilence bool, rootNote string) (uuid string, err error) {
func generateUserData(u string, startStop []float64, patchType string, removeSilence bool, rootNote string, splices int) (uuid string, err error) {
log.Debug(u, startStop)
log.Debug(patchType)
if startStop[1]-startStop[0] < 12 {
Expand All @@ -405,7 +409,7 @@ func generateUserData(u string, startStop []float64, patchType string, removeSil
startStop[1] = startStop[0] + 5.75
}

uuid = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%+v %+v %+v %+v %+v", patchType, u, startStop, removeSilence, rootNote))))
uuid = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%+v %+v %+v %+v %+v %+v", patchType, u, startStop, removeSilence, rootNote, splices))))

// create path to data
pathToData := path.Join("data", uuid)
Expand Down Expand Up @@ -495,7 +499,7 @@ func generateUserData(u string, startStop []float64, patchType string, removeSil
// generate patches
var segments [][]models.AudioSegment
if patchType == "drum" {
segments, err = audiosegment.SplitEqual(shortName, 12, 1)
segments, err = audiosegment.SplitEqual(shortName, 12, 1, splices)
if err != nil {
return
}
Expand Down Expand Up @@ -533,6 +537,7 @@ func generateUserData(u string, startStop []float64, patchType string, removeSil
IsSynthPatch: patchType == "synth",
RemoveSilence: removeSilence,
RootNote: rootNote,
Splices: splices,
})
err = ioutil.WriteFile(path.Join(pathToData, "metadata.json"), b, 0644)

Expand Down
56 changes: 31 additions & 25 deletions src/server/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -458,29 +458,33 @@
</select>
<label for="synthPatch">patch type:</label>
</div>
<div id="optionRemoveSilence" ((if $.Metadata.IsSynthPatch))style="display:none;"((end))>
<div id="optionRemoveSilence" ((if $.Metadata.IsSynthPatch))style="display:none;" ((end))>
<select name="removeSilence" id="removeSilence" style="text-align: center;">
<option value="no">no</option>
<option value="yes" ((if $.Metadata.RemoveSilence))selected((end))>yes</option>
</select>
<label for="removeSilence">remove silence?</label>
</div>
<div id="optionRootNote" ((if $.Metadata.IsSynthPatch))((else))style="display:none;"((end))>
<div id="optionNumberSplices" ((if $.Metadata.IsSynthPatch))style="display:none;" ((end))>
<input type="decimal" id="splices" name="splices" value="((if .Metadata))((.Metadata.Splices))((end))" style="min-width: 60%;">
<label for="optionNumberSplices">splices</label>
</div>
<div id="optionRootNote" ((if $.Metadata.IsSynthPatch))((else))style="display:none;" ((end))>
<select name="rootNote" id="rootNote" style="text-align: center;">
<option value="A" ((if eq $.Metadata.RootNote "A"))selected((end))>A</option>
<option value="A#" ((if eq $.Metadata.RootNote "A#"))selected((end))>A#</option>
<option value="B" ((if eq $.Metadata.RootNote "B"))selected((end))>B</option>
<option value="C" ((if eq $.Metadata.RootNote "C"))selected((end))>C</option>
<option value="C#" ((if eq $.Metadata.RootNote "C#"))selected((end))>C#</option>
<option value="D" ((if eq $.Metadata.RootNote "D"))selected((end))>D</option>
<option value="D#" ((if eq $.Metadata.RootNote "D#"))selected((end))>D#</option>
<option value="E" ((if eq $.Metadata.RootNote "E"))selected((end))>E</option>
<option value="F" ((if eq $.Metadata.RootNote "F"))selected((end))>F</option>
<option value="F#" ((if eq $.Metadata.RootNote "F#"))selected((end))>F#</option>
<option value="G" ((if eq $.Metadata.RootNote "G"))selected((end))>G</option>
<option value="G#" ((if eq $.Metadata.RootNote "G#"))selected((end))>G#</option>
<option value="A" ((if eq $.Metadata.RootNote "A" ))selected((end))>A</option>
<option value="A#" ((if eq $.Metadata.RootNote "A#" ))selected((end))>A#</option>
<option value="B" ((if eq $.Metadata.RootNote "B" ))selected((end))>B</option>
<option value="C" ((if eq $.Metadata.RootNote "C" ))selected((end))>C</option>
<option value="C#" ((if eq $.Metadata.RootNote "C#" ))selected((end))>C#</option>
<option value="D" ((if eq $.Metadata.RootNote "D" ))selected((end))>D</option>
<option value="D#" ((if eq $.Metadata.RootNote "D#" ))selected((end))>D#</option>
<option value="E" ((if eq $.Metadata.RootNote "E" ))selected((end))>E</option>
<option value="F" ((if eq $.Metadata.RootNote "F" ))selected((end))>F</option>
<option value="F#" ((if eq $.Metadata.RootNote "F#" ))selected((end))>F#</option>
<option value="G" ((if eq $.Metadata.RootNote "G" ))selected((end))>G</option>
<option value="G#" ((if eq $.Metadata.RootNote "G#" ))selected((end))>G#</option>
</select>
<label for="rootNote">root note:
<label for="rootNote">root note:
</label>
</div>
<div>
Expand Down Expand Up @@ -567,15 +571,17 @@
return true;
});

$('#synthPatch').change(function(e){
console.log(e.target.value);
if (e.target.value == "drum") {
$("#optionRemoveSilence").show();
$("#optionRootNote").hide();
} else {
$("#optionRemoveSilence").hide();
$("#optionRootNote").show();
}
$('#synthPatch').change(function(e) {
console.log(e.target.value);
if (e.target.value == "drum") {
$("#optionRemoveSilence").show();
$("#optionNumberSplices").show();
$("#optionRootNote").hide();
} else {
$("#optionRemoveSilence").hide();
$("#optionNumberSplices").hide();
$("#optionRootNote").show();
}
})
});
</script>
Expand Down Expand Up @@ -672,4 +678,4 @@
</body>

</html>
((end ))
((end ))

0 comments on commit 22d75f6

Please sign in to comment.