From 82139e2be34e13ac92432d9454fb6949d2ea5900 Mon Sep 17 00:00:00 2001
From: Antonio Ospite <ao2@ao2.it>
Date: Tue, 6 Dec 2016 16:19:34 +0100
Subject: [PATCH 1/1] Document how to create a more interesting VideoFont

Also add "contrib/ges-split-samples.py", an helper script to split
a video file according to labels defined in Audacity.

And add an example list of such a list of labels exported from Audacity,
it's in "contrib/keyboard videofont master C4 B5-samples.txt" and it can
be used to split in samples the example VideoFont at
https://youtu.be/7Btp80LPqRs
---
 README.md                                          | 47 ++++++++++-
 contrib/ges-split-samples.py                       | 91 ++++++++++++++++++++++
 .../keyboard videofont master C4 B5-samples.txt    | 26 +++++++
 3 files changed, 160 insertions(+), 4 deletions(-)
 create mode 100755 contrib/ges-split-samples.py
 create mode 100644 contrib/keyboard videofont master C4 B5-samples.txt

diff --git a/README.md b/README.md
index 4c4180c..9d2d858 100644
--- a/README.md
+++ b/README.md
@@ -10,17 +10,56 @@ vidi-timeline allows to create more easily videos like these:
 * Vittorio Saggiomo - Mario Bros Column Chromatography - https://youtu.be/mxi3z2vDV_0
 
 
-Examples of use
-===============
+How to create a VideoFont
+=========================
 
-vidi-timeline.py
-----------------
+Synthetic VideoFont
+-------------------
 
 Create a synthetinc VideoFont:
 
     $ ./create_test_videofont.py videofont/
 
 
+From a video recording
+----------------------
+
+A VideoFont can also be created by recording a video, and then splitting the
+recording in samples, one sample per note.
+
+For an example of a master VideoFont see
+[keyboard videofont master C4 B5](https://youtu.be/7Btp80LPqRs)
+
+A file like the one above can be analyzed with [Audacity][1] to find the start
+and the  end time of the individual samples:
+
+* use the `Analyze -> Sound Finder...` to find the samples;
+* use the [`Pitch Detect" plugin`][2] to find the pitch of the samples and name
+  them accordingly;
+* maybe add  an interval named "rest" to represent the absence of sound;
+* export the labels track with `File -> Export Labels...`;
+
+[1]: http://www.audacityteam.org/
+[2]: http://wiki.audacityteam.org/wiki/Nyquist_Analyze_Plug-ins#Pitch_Detect
+
+An example of such a file prodiced by audacity can be found in the `contrib/`
+directory and it can be used as follows:
+
+    $ youtube-dl -t https://youtu.be/7Btp80LPqRs
+    $ ./contrib/ges-split-samples.py \
+        "keyboard videofont master C4 B5-7Btp80LPqRs.mp4" \
+        "contrib/keyboard videofont master C4 B5-samples.txt" \
+        videofont/ > split.sh
+    $ mkdir videofont/
+    $ sh split.sh && rm split.sh
+
+
+Examples of use
+===============
+
+vidi-timeline.py
+----------------
+
 Play the timeline from a MIDI file using the samples from the VideoFont:
 
     $ ./vidi-timeline.py examples/Beyer\ Op.\ 101\ -\ Exercise\ 008.midi videofont/
diff --git a/contrib/ges-split-samples.py b/contrib/ges-split-samples.py
new file mode 100755
index 0000000..167c4f1
--- /dev/null
+++ b/contrib/ges-split-samples.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python3
+#
+# ges-split-samples - generate commands to split a video file into samples
+#
+# Copyright (C) 2016  Antonio Ospite <ao2@ao2.it>
+#
+# This program 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
+# (at your option) any later version.
+#
+# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import sys
+
+CLIP_FORMAT = "video/webm:video/x-vp8:audio/x-vorbis"
+CLIP_FILE_EXTENSION = "webm"
+CLIP_FILENAME_TEMPLATE = "sample_%s.%s"
+
+
+def parse_audacity_labels(samples_list_filename):
+    """Parse labels exported from Audacity.
+
+    NOTE: Audacity uses the current user locale when exporting the labels, but
+    this way there is no portable way to parse the output, so we just assume
+    here that the C locale was used.
+
+    Basically run "LANG=C audacity" before exporting the data.
+    """
+    samples = []
+
+    with open(samples_list_filename, "r") as samples_list_file:
+        for row in samples_list_file:
+            if row.startswith('#'):
+                continue
+
+            start_time, end_time, sample_name = row.split()
+
+            start_time = float(start_time)
+            end_time = float(end_time)
+
+            samples.append((sample_name, start_time, end_time))
+
+    return samples
+
+
+def usage():
+    print("usage: %s <video_file> <samples_list_file> <destination_dir>"
+          % os.path.basename(sys.argv[0]))
+    print("sample_list_file is in the format used by Audacity when exporting Label Tracks")
+
+
+def main():
+    if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
+        usage()
+        return 0
+
+    if len(sys.argv) < 4:
+        usage()
+        return 1
+
+    master_file = sys.argv[1]
+    samples_list_filename = sys.argv[2]
+    destination_dir = sys.argv[3]
+
+    samples = parse_audacity_labels(samples_list_filename)
+    for sample_name, start_time, end_time in samples:
+        duration = round(end_time - start_time, 2)
+        clip_filename = CLIP_FILENAME_TEMPLATE % (sample_name,
+                                                  CLIP_FILE_EXTENSION)
+        clip_path = os.path.join(destination_dir, clip_filename)
+        print("ges-launch-1.0 +clip \"%s\" inpoint=%s duration=%s -o \"%s\" --format=\"%s\"" %
+              (master_file, start_time, duration, clip_path, CLIP_FORMAT))
+
+        if sample_name == "rest":
+            rest_sample_filename = CLIP_FILENAME_TEMPLATE % (sample_name, "png")
+            rest_sample_path = os.path.join(destination_dir,
+                                            rest_sample_filename)
+            print("gst-launch-1.0 filesrc location=\"%s\" ! decodebin ! videoconvert ! pngenc snapshot=1 ! filesink location=\"%s\"" %
+                  (clip_path, rest_sample_path))
+
+
+if __name__ == "__main__":
+    sys.exit(main())
diff --git a/contrib/keyboard videofont master C4 B5-samples.txt b/contrib/keyboard videofont master C4 B5-samples.txt
new file mode 100644
index 0000000..bdc157e
--- /dev/null
+++ b/contrib/keyboard videofont master C4 B5-samples.txt	
@@ -0,0 +1,26 @@
+# Samples list in https://youtu.be/7Btp80LPqRs
+4.050000	11.140000	C4
+13.290000	21.060000	C#4
+23.850000	30.210000	D4
+33.710000	38.360000	Eb4
+43.120000	47.430000	E4
+51.870000	56.010000	F4
+60.830000	65.490000	F#4
+69.750000	73.940000	G4
+78.850000	82.950000	Ab4
+86.920000	90.720000	A4
+96.110000	100.270000	Bb4
+104.690000	108.460000	B4
+111.820000	115.890000	C5
+118.920000	122.760000	C#5
+125.790000	130.070000	D5
+133.670000	138.080000	Eb5
+141.280000	146.030000	E5
+149.740000	154.330000	F5
+158.140000	163.260000	F#5
+166.370000	169.400000	G5
+171.810000	175.920000	Ab5
+178.050000	181.720000	A5
+184.510000	188.540000	Bb5
+192.340000	196.340000	B5
+199.000000	200.850000	rest
-- 
2.1.4