PHP Classes

File: html_example.php

Recommend this page to a friend!
  Classes of Arturs Sosins   Music theory   html_example.php   Download  
File: html_example.php
Role: Example script
Content type: text/plain
Description: Example with HTML forms
Class: Music theory
Generate music scales and chords
Author: By
Last change: credits changed
Date: 12 years ago
Size: 12,034 bytes
 

Contents

Class file image Download
<?php /************************************************************* * This script is developed by Arturs Sosins aka ar2rsawseen, http://webcodingeasy.com * Fee free to distribute and modify code, but keep reference to its creator * * This class implements music theory for generating scales and chords * based on interval patterns between notes. User can add custom scale and chord pattern. * This class can generate scale notes based on provided scale name and type, * generate chord notes based on provided chord name and type, * transpose scales, transpose chords, * generate all chords that include provided notes, * generate all scales that include provided notes, * * For more information, examples and online documentation visit: * http://webcodingeasy.com/PHP-classes/Implement-music-theory-to-generate-scale-and-chord-notes **************************************************************/ /*************************************** * Some explanations of terms: * note name = chord name = scale name //they all have the same value like C minor where C is note name, minor is chord or scale type * chord = chord name + chord type * scale = scale name + scale type ***************************************/ session_start(); //declaring class instance include("./music_theory.php"); $mt = new music_theory(); /******************************* * setting notation and storing it in session *******************************/ if(!isset($_SESSION["notation"])) { $_SESSION["notation"] = "set_sharp"; } if(isset($_POST["notation"])) { if(in_array($_POST["notation"], array("set_sharp", "set_flat"))) { $_SESSION["notation"] = $_POST["notation"]; } } //using stored notation $mt->$_SESSION["notation"](); /*******************************/ ?> <style type='text/css'> #chords td { text-align: center; } </style> <?php /******************************* * changing notation sharp/flat *******************************/ echo "<form action='' method='post'>"; echo "<p>Change notation: <select name='notation'>"; echo "<option value='set_sharp'>Use sharp notation</option>"; echo "<option value='set_flat'>Use flat notation</option>"; echo "</select>&nbsp;<input type='submit' value='Submit'/></p>"; echo "</form>"; /******************************/ //getting all notes $notes = $mt->get_notes(); //getting all defined chord types $chord_types = $mt->get_chord_types(); //getting all defined scale types $scale_types = $mt->get_scale_types(); /********************************************** * Get Chord notes from chord name (note name and chord type) **********************************************/ echo "<fieldset><legend>Get Chord notes by name</legend>"; echo "<form action='' method='post'>"; echo "<table border='1' id='chords'>"; echo "<tr><td>Select Chord name</td><td>Select Chord type</td><td>Result:</td></tr>"; echo "<tr><td>"; echo "<select name='chord_name'>"; //outputting all notes foreach($notes as $note) { echo "<option value='".$note."'>".$note."</option>"; } echo "</select>"; echo "</td><td>"; echo "<select name='chord_type'>"; //outputting all chord types foreach($chord_types as $chord) { echo "<option value='".$chord."'>".$chord."</option>"; } echo "</select>"; echo "</td><td>"; //if isset post outputting result if(isset($_POST["chord_name"]) && isset($_POST["chord_type"])) { //getting array with all chord notes using note name and chord type from sleect input $arr = $mt->get_chord_by_name($_POST["chord_name"], $_POST["chord_type"]); //check if there were any errors $errors = $mt->get_errors(); if(!empty($errors)) { foreach($errors as $error) { echo "<p>".$error."</p>"; } } else { echo "<p>"; foreach($arr as $val) { //outputting notes one by one echo $val."&nbsp;"; } echo "</p>"; } } else { echo "&nbsp;"; } echo "</td></tr>"; echo "<tr><td colspan='3'><input type='submit' value='Submit'/></td></tr>"; echo "</table>"; echo "</form>"; echo "</fieldset>"; /**********************************************/ /********************************************** * Get Scale notes from scale name (note name and scale type) **********************************************/ echo "<fieldset><legend>Get Scale notes by name</legend>"; echo "<form action='' method='post'>"; echo "<table border='1' id='scales'>"; echo "<tr><td>Select Scale name</td><td>Select Scale type</td><td>Result</td></tr>"; echo "<tr><td>"; echo "<select name='scale_name'>"; //outputting all notes foreach($notes as $note) { echo "<option value='".$note."'>".$note."</option>"; } echo "</select>"; echo "</td><td>"; echo "<select name='scale_type'>"; //outputting all scale types foreach($scale_types as $scale) { echo "<option value='".$scale."'>".$scale."</option>"; } echo "</select>"; echo "</td><td>"; //if isset post outputting result if(isset($_POST["scale_name"]) && isset($_POST["scale_type"])) { //getting array with all scale notes using note name and scale type from sleect input $arr = $mt->get_scale_by_name($_POST["scale_name"], $_POST["scale_type"]); //check if there were any errors $errors = $mt->get_errors(); if(!empty($errors)) { foreach($errors as $error) { echo "<p>".$error."</p>"; } } else { echo "<p>"; foreach($arr as $val) { //outputting notes one by one echo $val."&nbsp;"; } echo "</p>"; } } else { echo "&nbsp;"; } echo "</td></tr>"; echo "<tr><td colspan='3'><input type='submit' value='Submit'/></td></tr>"; echo "</table>"; echo "</form>"; echo "</fieldset>"; /*********************************************/ /********************************************** * Get Chord name or Scale name from notes **********************************************/ echo "<fieldset><legend>Get Chords/Scales by notes </legend>"; echo "<form action='' method='post'>"; echo "<table border='1' id='chords'>"; echo "<tr><td>Select multiple notes</td><td>Search for</td><td>Result</td></tr>"; echo "<tr><td>"; echo "<select name='note_names[]' multiple='multiple' size='8'>"; //outputting all notes foreach($notes as $note) { echo "<option value='".$note."'>".$note."</option>"; } echo "</select>"; echo "</td><td>"; echo "<select name='search_type'>"; //selecting which method to use //get chords or get scales echo "<option value='get_chords_by_notes'>Chords</option>"; echo "<option value='get_scales_by_notes'>Scales</option>"; echo "</select>"; echo "</td><td>"; if(isset($_POST["note_names"]) && isset($_POST["search_type"])) { if(in_array($_POST["search_type"], array("get_chords_by_notes", "get_scales_by_notes"))) { //getting from selected method by passing array with note names $arr = $mt->$_POST["search_type"]($_POST["note_names"]); echo "<table border='1' cellpadding='5'>"; $col = 0; foreach($arr as $ch) { if($col % 8 == 0) { echo "<tr>"; } $col++; echo "<td style='text-align:left; vertical-align: top;'>"; //outputting chord or scale name iwth type echo $ch["name"]." ".$ch["type"].":"; if($_POST["search_type"] == "get_chords_by_notes") { //if looking for chords, then getting all notes for eath chord $ch_notes = $mt->get_chord_by_name($ch["name"],$ch["type"]); } else { //if looking for scales, then getting all notes for each scale $ch_notes = $mt->get_scale_by_name($ch["name"],$ch["type"]); } echo "<ul>"; foreach($ch_notes as $ch_note) { //outputing notes echo "<li>".$ch_note."</li>"; } echo "</ul>"; echo "</td>"; if($col % 8 == 0) { echo "</tr>"; } } while($col % 8 != 0) { $col++; echo "<td></td>"; } echo "</tr>"; echo "</table>"; } } else { echo "&nbsp;"; } echo "</td></tr>"; echo "<tr><td colspan='3'><input type='submit' value='Submit'/></td></tr>"; echo "</table>"; echo "</form>"; echo "</fieldset>"; /******************************************/ /********************************************** * Get All chords that can be played along inputed scale **********************************************/ echo "<fieldset><legend>Get Chords by scale</legend>"; echo "<form action='' method='post'>"; echo "<table border='1' id='chords'>"; echo "<tr><td>Select Scale:</td><td>Result</td></tr>"; echo "<tr><td>"; echo "<select name='scale_for_chords'>"; //outputting all notes foreach($notes as $note) { //mixing them with scale types using delimiter "_" foreach($scale_types as $scale) { echo "<option value='".$note."_".$scale."'>".$note." ".$scale."</option>"; } } echo "</select>"; echo "</td><td>"; //if isset post outputting result if(isset($_POST["scale_for_chords"])) { //exploding to note name and scale type using same delimiter "_" $sc_data = explode("_", $_POST["scale_for_chords"]); //getting chord by passing scale name and scale type $arr = $mt->get_chords_by_scale($sc_data[0], $sc_data[1]); echo "<table border='1' cellpadding='5'>"; $col = 0; foreach($arr as $ch) { if($col % 8 == 0) { echo "<tr>"; } $col++; echo "<td style='text-align:left; vertical-align: top;'>"; echo $ch["name"]." ".$ch["type"].":"; //getting all notes for chord $ch_notes = $mt->get_chord_by_name($ch["name"],$ch["type"]); echo "<ul>"; foreach($ch_notes as $ch_note) { echo "<li>".$ch_note."</li>"; } echo "</ul>"; echo "</td>"; if($col % 8 == 0) { echo "</tr>"; } } while($col % 8 != 0) { $col++; echo "<td></td>"; } echo "</tr>"; echo "</table>"; } else { echo "&nbsp;"; } echo "</td></tr>"; echo "<tr><td colspan='3'><input type='submit' value='Submit'/></td></tr>"; echo "</table>"; echo "</form>"; echo "</fieldset>"; /*************************************************/ /********************************************** * Get all scales that suits specified chord progressions **********************************************/ echo "<fieldset><legend>Get Scales by chords</legend>"; echo "<form action='' method='post'>"; echo "<table border='1' id='chords'>"; echo "<tr><td>Select multiple chords:</td><td>Result</td></tr>"; echo "<tr><td>"; echo "<select name='chords_for_scale[]' multiple='multiple' size='20'>"; //outputting note nams foreach($notes as $note) { //mixing with chord types using delimiter "_" foreach($chord_types as $chord) { echo "<option value='".$note."_".$chord."'>".$note." ".$chord."</option>"; } } echo "</select>"; echo "</td><td>"; if(isset($_POST["chords_for_scale"])) { $arr = array(); foreach($_POST["chords_for_scale"] as $ch) { //as user selected multiple chords, explode each chord using same delimiter "_" $ch_data = explode("_", $ch); $cur = sizeof($arr); //forming properly structured array for get_scales_by_chords method $arr[$cur]["name"] = $ch_data[0]; $arr[$cur]["type"] = $ch_data[1]; } //getting all scales that suits specified chords $res = $mt->get_scales_by_chords($arr); echo "<table border='1' cellpadding='5'>"; $col = 0; foreach($res as $ch) { if($col % 8 == 0) { echo "<tr>"; } $col++; echo "<td style='text-align:left; vertical-align: top;'>"; echo $ch["name"]." ".$ch["type"].":"; //getting notes for each scale $ch_notes = $mt->get_scale_by_name($ch["name"],$ch["type"]); echo "<ul>"; foreach($ch_notes as $ch_note) { echo "<li>".$ch_note."</li>"; } echo "</ul>"; echo "</td>"; if($col % 8 == 0) { echo "</tr>"; } } while($col % 8 != 0) { $col++; echo "<td></td>"; } echo "</tr>"; echo "</table>"; } else { echo "&nbsp;"; } echo "</td></tr>"; echo "<tr><td colspan='3'><input type='submit' value='Submit'/></td></tr>"; echo "</table>"; echo "</form>"; echo "</fieldset>"; /***********************************************/ ?>