Robbie starts the development of a PHP tool to read all MIDI files from a folder and play them.
Topics Covered:
sudo usermod -a -G pulse-access www-data
<?php
  ///////////////////////////////////////////////////////////////////////////
  // This script was written live on Category5 Technology TV episode 326.  //
  // Check out the episode at http://www.category5.tv/episodes/326.php     //
  // Note: While live on the air, we couldn't get it to play--turns out    //
  //       it was a pulseaudio issue, fixed with the following command...  //
  //       Assuming your apache (like ours) is running as www-data user,   //
  //       and that your system is using PulseAudio, make it go with:      //
  //       sudo usermod -a -G pulse-access www-data                        //
  //       MAY BE A GOOD IDEA TO REBOOT at that point, just to be sure...  //
  ///////////////////////////////////////////////////////////////////////////
  
  $folder = './mid';
  if ($handle = opendir($folder)) {
    while (false !== ($entry = readdir($handle))) {
      if (stristr($entry, '.mid')) {
        $songs[] = $entry;
      }
    }
  }
  if (is_array($songs)) {
    foreach ($songs as $id => $song) {
      if ($_POST['song'] == $id) {
        exec('killall -9 aplaymidi');
        exec('aplaymidi -p 128 "' . $folder . '/' . $song . '" > /dev/null 2>/dev/null &');
      }
    }
    echo '<form method="post">';
    echo '<select name="song">';
    foreach ($songs as $id => $song) {
     echo '<option value="' . $id . '"> ' . $song . ' </option>' . PHP_EOL;
    }
    echo '</select>';
    echo '<input type="submit" value="Play" />';
    echo '</form>';
  }
?><?php
  ///////////////////////////////////////////////////////////////////////////
  // This script is a cronjob alternative as described on Category5        //
  // Technology TV episode 326 and based on the script created then.       //
  // Check out the episode at http://www.category5.tv/episodes/326.php     //
  // Note: this is built to be run alongside a cronjob as follows:         //
  //       */1 * * * * /tmp/midi.sh                                        //
  // midi.sh won't exist until you press play (it's automatically created) //
  // Due to the nature of cron, the song will start playing as late as     //
  // one minute after clicking "play".                                     //
  ///////////////////////////////////////////////////////////////////////////
  
  $folder = '/var/www/mid';
  if ($handle = opendir($folder)) {
    while (false !== ($entry = readdir($handle))) {
      if (stristr($entry, '.mid')) {
        $songs[] = $entry;
      }
    }
  } 
  $command = '#!/bin/bash' . PHP_EOL;
  if (is_array($songs)) {
    foreach ($songs as $id => $song) {
      if ($_POST['song'] == $id) {
        $command .= '/usr/bin/killall -9 aplaymidi' . PHP_EOL;
        $command .= '/usr/bin/aplaymidi -p 128:0 "' . $folder . '/' . $song . '" > /dev/null 2>/dev/null &' . PHP_EOL;
      }
    }
    $filename = '/tmp/midi.sh';
    $command .= 'rm -f ' . $filename;
    file_put_contents($filename, $command);
    exec('chmod +x ' . $filename);
    
    echo '<form method="post">';
    echo '<select name="song">';
    foreach ($songs as $id => $song) {
     echo '<option value="' . $id . '"> ' . $song . ' </option>' . PHP_EOL;
    }
    echo '</select>';
    echo '<input type="submit" value="Play" />';
    echo '</form>';
}
?>Lost your password? Please enter your email address. You will receive a link to create a new password.
 Mastodon
 Mastodon
             Discord
 Discord