This is a simple example of how to play audio. * `tr.php` searches (`tr.php?word1/word2` words are separated by slashes, by default matches any words, unless `.a` is one of the path components, then it matches all) * `playmusic.php` presents an HTML page with an embedded audio player * `playaudio.php` streams the audio file ## playaudio.php ```php Audio: <?php echo $fn; ?>

prepare("SELECT paths FROM tracks_by_word WHERE word = :word"); $stmt->bindValue(":word",$word); $result = $stmt->execute(); $got = $result->fetchArray(SQLITE3_NUM); if( $got === false ) { echo "got false"; return []; } $got = $got[0]; $paths = json_decode($got); return $paths; } $qs = $_SERVER['QUERY_STRING']; $qs = strtolower($qs); $ws = explode("/",$qs); $all = false; $words = []; foreach($ws as $word) { if( $word == ".a" ) { $all = true; } else if( $word !== "" ) { array_push($words,$word); } } if( count($words) == 0 ) { echo "no words"; exit(); } if( $all ) { $found = []; echo "all words: ".implode(", ",$words); $word1 = array_shift($words); $f = get1($word1); foreach($f as $p) { $found[$p] = true; } foreach($words as $word) { $f = get1($word); $g = []; foreach($f as $x) { $g[$x] = true; } foreach($found as $k => $v) { if( !array_key_exists($k,$g) ) { unset($found[$k]); } } } } else { echo "any words: ".implode(", ",$words); $found = []; foreach($words as $word) { $f = get1($word); foreach($f as $p) { $found[$p] = true; } } } // print results { $found = array_keys($found); if( count($found) == 0 ) { echo "found nothing"; exit(); } sort($found); echo "
    \n"; foreach($found as $path) { if( preg_match('@\.\./@',$path) ) { echo ".. in path"; exit(); } $p = urlencode($path); $url = "/playmusic.php?$p"; echo "
  1. $path
  2. \n"; } echo "
\n"; } ``` ## mkwordidx Takes file index (produced using `find`), splits at non-alphanumeric into words, converts to lowercase, and makes a dict mapping word to path. ```py import json, re from collections import defaultdict d = defaultdict(list) a = open("music.index.txt").read().rstrip().splitlines() for x in a: if x.startswith("./"): x = x[2:] y = x.lower() y = re.sub(r"[^a-zA-A0-9]+","_",y) y = re.sub(r"__+","_",y) xs = y.split("_") for word in xs: if word != "": d[word].append(x) with open("music.byword.json","wt") as f: json.dump(d,f) ``` ## mkwordsql Reads the JSON from above and inserts it into an sqlite3 database. ```py import json, sqlite3, os with open("music.byword.json") as f: d = json.load(f) dbfn = "music.byword.db" if os.path.exists(dbfn): os.unlink(dbfn) con = sqlite3.connect(dbfn) cur = con.cursor() cur.execute("CREATE TABLE tracks_by_word ( word PRIMARY KEY, paths )") data = [ (word, json.dumps(paths)) for word, paths in d.items() ] cur.executemany("INSERT INTO tracks_by_word ( word, paths ) VALUES ( ?, ? )", data ) con.commit() con.close() ``` ## Find The list of filenames is made by ```bash find . -type f | grep -P '(mp3|m4a|wav|flac)$' > music.index.txt ```