public double generate_Jazz(SongParameters paramets) { Random randomizer = new Random(paramets.seed); int mode; // 0 = Major 1 = Minor String key; String gen; String timeSigPattern = ""; //Simple or Compound Meter int timeSigQuant = 0; // 2 = Duple, 3 = Triple, etc int numpatterns = 0; String[] notes = { "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" }; //array of all note values //Select Mode mode = randomizer.Next(2); //Select Key key = notes[randomizer.Next(12)]; //Set genre gen = paramets.genre; //Now also sets the genre Song output = new Song(paramets.tempo, key, paramets.genre); Console.Out.WriteLine(key); Console.Out.WriteLine(paramets.genre); int randOutput = randomizer.Next(2); switch (randOutput) { case 0: timeSigPattern = "Simple"; break; case 1: timeSigPattern = "Compound"; break; } timeSigQuant = randomizer.Next(3) + 2; Console.Out.WriteLine(timeSigPattern + " " + timeSigQuant); //numpatterns is a value between 2 and 6 numpatterns = randomizer.Next(4) + 2; List<Song.SongSegment> patterns = new List<Song.SongSegment>(); for (int i = 0; i < numpatterns; i++) { Song.SongSegment thisSection = new Song.SongSegment(); randOutput = randomizer.Next(8) + 1; int measures = randOutput * 4; int rep = 0; while (rep == 0 || (measures % rep != 0)) { rep = (randomizer.Next(8) + 1) * 4; } SongPattern inGeneration = new SongPattern(measures, rep); bool prevWasHalf = false; for (int j = 0; j < inGeneration.repeatEvery / 4; j++) { int numChords = randomizer.Next(6) + 1; String chord = ""; if (numChords == 1) { chord = "1"; } if (numChords == 2) { if (j == 0 || prevWasHalf) { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(2); switch (randOutput) { case 0: chord = "15"; break; case 1: chord = "1"; break; } } else { chord = "1"; } } else { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(9); switch (randOutput) { case 0: chord = "16"; break; case 1: chord = "56"; break; case 2: chord = "76"; break; case 3: chord = "15"; break; case 4: chord = "45"; break; case 5: chord = "25"; break; case 6: chord = "51"; break; case 7: chord = "41"; break; case 8: chord = "71"; break; } } else { randOutput = randomizer.Next(3); switch (randOutput) { case 0: chord = "51"; break; case 1: chord = "71"; break; } } } } if (numChords == 3) { if (j == 0 || prevWasHalf) { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(6); switch (randOutput) { case 0: chord = "145"; break; case 1: chord = "125"; break; case 2: chord = "156"; break; case 3: chord = "151"; break; case 4: chord = "171"; break; case 5: chord = "141"; break; } } else { randOutput = randomizer.Next(3); switch (randOutput) { case 0: chord = "151"; break; case 1: chord = "171"; break; case 2: chord = "141"; break; } } } else { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(17); switch (randOutput) { case 0: chord = "125"; break; case 1: chord = "145"; break; case 2: chord = "625"; break; case 3: chord = "645"; break; case 4: chord = "345"; break; case 5: chord = "156"; break; case 6: chord = "256"; break; case 7: chord = "456"; break; case 8: chord = "151"; break; case 9: chord = "251"; break; case 10: chord = "451"; break; case 11: chord = "171"; break; case 12: chord = "271"; break; case 13: chord = "471"; break; case 14: chord = "141"; break; case 15: chord = "641"; break; case 16: chord = "341"; break; } } else { randOutput = randomizer.Next(9); switch (randOutput) { case 0: chord = "151"; break; case 1: chord = "251"; break; case 2: chord = "451"; break; case 3: chord = "171"; break; case 4: chord = "271"; break; case 5: chord = "471"; break; case 6: chord = "141"; break; case 7: chord = "641"; break; case 8: chord = "341"; break; } } } } if (numChords == 4) { if (j == 0 || prevWasHalf) { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(12); switch (randOutput) { case 0: chord = "1625"; break; case 1: chord = "1645"; break; case 2: chord = "1425"; break; case 3: chord = "1345"; break; case 4: chord = "1256"; break; case 5: chord = "1456"; break; case 6: chord = "1641"; break; case 7: chord = "1341"; break; case 8: chord = "1271"; break; case 9: chord = "1471"; break; case 10: chord = "1251"; break; case 11: chord = "1451"; break; } } else { randOutput = randomizer.Next(6); switch (randOutput) { case 0: chord = "1641"; break; case 1: chord = "1341"; break; case 2: chord = "1271"; break; case 3: chord = "1471"; break; case 4: chord = "1251"; break; case 5: chord = "1451"; break; } } } else { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(29); switch (randOutput) { case 0: chord = "1625"; break; case 1: chord = "1645"; break; case 2: chord = "3625"; break; case 3: chord = "3645"; break; case 4: chord = "1425"; break; case 5: chord = "1345"; break; case 6: chord = "3425"; break; case 7: chord = "6425"; break; case 8: chord = "1256"; break; case 9: chord = "1456"; break; case 10: chord = "6256"; break; case 11: chord = "6456"; break; case 12: chord = "3456"; break; case 13: chord = "4256"; break; case 14: chord = "1641"; break; case 15: chord = "3641"; break; case 16: chord = "1341"; break; case 17: chord = "1271"; break; case 18: chord = "6271"; break; case 19: chord = "1471"; break; case 20: chord = "6471"; break; case 21: chord = "3471"; break; case 22: chord = "1251"; break; case 23: chord = "6251"; break; case 24: chord = "1451"; break; case 25: chord = "6451"; break; case 26: chord = "3451"; break; case 27: chord = "4251"; break; case 28: chord = "4271"; break; } } else { randOutput = randomizer.Next(15); switch (randOutput) { case 0: chord = "1641"; break; case 1: chord = "3641"; break; case 2: chord = "1341"; break; case 3: chord = "1271"; break; case 4: chord = "6271"; break; case 5: chord = "1471"; break; case 6: chord = "6471"; break; case 7: chord = "3471"; break; case 8: chord = "1251"; break; case 9: chord = "6251"; break; case 10: chord = "1451"; break; case 11: chord = "6451"; break; case 12: chord = "3451"; break; case 13: chord = "4251"; break; case 14: chord = "4271"; break; } } } } if (numChords == 5) { if (j == 0 || prevWasHalf) { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(17); switch (randOutput) { case 0: chord = "13625"; break; case 1: chord = "13645"; break; case 2: chord = "13425"; break; case 3: chord = "16425"; break; case 4: chord = "16256"; break; case 5: chord = "16456"; break; case 6: chord = "13456"; break; case 7: chord = "14256"; break; case 8: chord = "13641"; break; case 9: chord = "16271"; break; case 10: chord = "16471"; break; case 11: chord = "13471"; break; case 12: chord = "16251"; break; case 13: chord = "16451"; break; case 14: chord = "13451"; break; case 15: chord = "14521"; break; case 16: chord = "14271"; break; } } else { randOutput = randomizer.Next(9); switch (randOutput) { case 0: chord = "13641"; break; case 1: chord = "16271"; break; case 2: chord = "16471"; break; case 3: chord = "13471"; break; case 4: chord = "16251"; break; case 5: chord = "16451"; break; case 6: chord = "13451"; break; case 7: chord = "14521"; break; case 8: chord = "14271"; break; } } } else { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(30); switch (randOutput) { case 0: chord = "13625"; break; case 1: chord = "13645"; break; case 2: chord = "13425"; break; case 3: chord = "16425"; break; case 4: chord = "36425"; break; case 5: chord = "16256"; break; case 6: chord = "36256"; break; case 7: chord = "16456"; break; case 8: chord = "36456"; break; case 9: chord = "13456"; break; case 10: chord = "14256"; break; case 11: chord = "64256"; break; case 12: chord = "34256"; break; case 13: chord = "13641"; break; case 14: chord = "16271"; break; case 15: chord = "36271"; break; case 16: chord = "16471"; break; case 17: chord = "36471"; break; case 18: chord = "13471"; break; case 19: chord = "16251"; break; case 20: chord = "36251"; break; case 21: chord = "16451"; break; case 22: chord = "36451"; break; case 23: chord = "13451"; break; case 24: chord = "14251"; break; case 25: chord = "34251"; break; case 26: chord = "64251"; break; case 27: chord = "14271"; break; case 28: chord = "34271"; break; case 29: chord = "64271"; break; } } else { randOutput = randomizer.Next(17); switch (randOutput) { case 0: chord = "13641"; break; case 1: chord = "16271"; break; case 2: chord = "36271"; break; case 3: chord = "16471"; break; case 4: chord = "36471"; break; case 5: chord = "13471"; break; case 6: chord = "16251"; break; case 7: chord = "36251"; break; case 8: chord = "16451"; break; case 9: chord = "36451"; break; case 10: chord = "13451"; break; case 11: chord = "14251"; break; case 12: chord = "34251"; break; case 13: chord = "64251"; break; case 14: chord = "14271"; break; case 15: chord = "34271"; break; case 16: chord = "64271"; break; } } } } if (numChords == 6) { if (j == 0 || prevWasHalf) { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(13); switch (randOutput) { case 0: chord = "136425"; break; case 1: chord = "136256"; break; case 2: chord = "136456"; break; case 3: chord = "164256"; break; case 4: chord = "134256"; break; case 5: chord = "136271"; break; case 6: chord = "136471"; break; case 7: chord = "136251"; break; case 8: chord = "136451"; break; case 9: chord = "134251"; break; case 10: chord = "164251"; break; case 11: chord = "134271"; break; case 12: chord = "164271"; break; } } else { randOutput = randomizer.Next(8); switch (randOutput) { case 0: chord = "136271"; break; case 1: chord = "136471"; break; case 2: chord = "136251"; break; case 3: chord = "136451"; break; case 4: chord = "134251"; break; case 5: chord = "164251"; break; case 6: chord = "134271"; break; case 7: chord = "164271"; break; } } } else { if (j != (inGeneration.repeatEvery / 4) - 1) { randOutput = randomizer.Next(16); switch (randOutput) { case 0: chord = "136425"; break; case 1: chord = "136256"; break; case 2: chord = "136456"; break; case 3: chord = "164256"; break; case 4: chord = "364256"; break; case 5: chord = "134256"; break; case 6: chord = "136271"; break; case 7: chord = "136471"; break; case 8: chord = "136251"; break; case 9: chord = "136451"; break; case 10: chord = "134251"; break; case 11: chord = "164251"; break; case 12: chord = "364251"; break; case 13: chord = "134271"; break; case 14: chord = "164271"; break; case 15: chord = "364271"; break; } } else { randOutput = randomizer.Next(10); switch (randOutput) { case 0: chord = "136271"; break; case 1: chord = "136471"; break; case 2: chord = "136251"; break; case 3: chord = "136451"; break; case 4: chord = "134251"; break; case 5: chord = "164251"; break; case 6: chord = "364251"; break; case 7: chord = "134271"; break; case 8: chord = "164271"; break; case 9: chord = "364271"; break; } } } } int sumRhythm = 0; numChords = chord.Length; int rhythm = 0; int measureLen = 0; //TODO Add rules for generating rhythm for (int count = 0; count < numChords; count++) { if (timeSigPattern.Equals("Simple")) { if (timeSigQuant == 2) { measureLen = 4; } if (timeSigQuant == 3) { measureLen = 6; } if (timeSigQuant == 4) { measureLen = 8; } if (count == numChords - 6) { rhythm = randomizer.Next(measureLen) + 1; } if (count == numChords - 5) { if (sumRhythm % 2 != 0) { do { rhythm = randomizer.Next(2 * measureLen - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0) || (rhythm + sumRhythm <= measureLen && (rhythm + sumRhythm) % 2 != 0)); } else { do { rhythm = randomizer.Next(2 * measureLen - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && rhythm + sumRhythm % measureLen != 0)); } } if (count == numChords - 4) { if (sumRhythm % 2 != 0) { do { rhythm = randomizer.Next(3 * measureLen - 1 - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0) || (rhythm + sumRhythm <= (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % 2 != 0)); } else { do { rhythm = randomizer.Next(3 * measureLen - 1 - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0)); } } if (count == numChords - 3) { if (sumRhythm % 2 != 0) { do { rhythm = randomizer.Next(3 * measureLen - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0) || (rhythm + sumRhythm <= (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % 2 != 0)); } else { do { rhythm = randomizer.Next(3 * measureLen - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0)); } } if (count == numChords - 2) { if (sumRhythm % 2 != 0) { do { rhythm = randomizer.Next(4 * measureLen - 1 - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0) || (rhythm + sumRhythm <= (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % 2 != 0)); } else { do { rhythm = randomizer.Next(4 * measureLen - 1 - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0)); } } if (count == numChords - 1) { rhythm = 4 * measureLen - sumRhythm; } } else { if (timeSigQuant == 2) { measureLen = 6; } if (timeSigQuant == 3) { measureLen = 9; } if (timeSigQuant == 4) { measureLen = 12; } if (count == numChords - 6) { rhythm = randomizer.Next(measureLen) + 1; } if (count == numChords - 5) { if (sumRhythm % 3 != 0) { do { rhythm = randomizer.Next(2 * measureLen - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0) || (rhythm + sumRhythm <= measureLen && (rhythm + sumRhythm) % 3 != 0)); } else { do { rhythm = randomizer.Next(2 * measureLen - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && rhythm + sumRhythm % measureLen != 0)); } } if (count == numChords - 4) { if (sumRhythm % 3 != 0) { do { rhythm = randomizer.Next(3 * measureLen - 1 - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0) || (rhythm + sumRhythm <= (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % 3 != 0)); } else { do { rhythm = randomizer.Next(3 * measureLen - 1 - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0)); } } if (count == numChords - 3) { if (sumRhythm % 3 != 0) { do { rhythm = randomizer.Next(3 * measureLen - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0) || (rhythm + sumRhythm <= (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % 3 != 0)); } else { do { rhythm = randomizer.Next(3 * measureLen - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0)); } } if (count == numChords - 2) { if (sumRhythm % 3 != 0) { do { rhythm = randomizer.Next(4 * measureLen - 1 - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0) || (rhythm + sumRhythm <= (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % 3 != 0)); } else { do { rhythm = randomizer.Next(4 * measureLen - 1 - sumRhythm) + 1; } while ((rhythm + sumRhythm > (sumRhythm + (measureLen - sumRhythm % measureLen)) && (rhythm + sumRhythm) % measureLen != 0)); } } if (count == numChords - 1) { rhythm = 4 * measureLen - sumRhythm; } } thisSection.chordPattern.Add(generateJazzChord(mode, key, chord[count], 2 * rhythm)); sumRhythm += rhythm; } /*if (chord[chord.Length - 1] == '5') { prevWasHalf = true; }*/ } composeJazzMelody(thisSection, randomizer, key, mode, timeSigPattern, timeSigQuant, paramets.tempo); patterns.Add(thisSection); } const int MAXNUMSECTIONS = 8; //totalSections is a random number between 1 and 8 int totalSections = randomizer.Next(MAXNUMSECTIONS) + 1; //numReps denotes how many time any section can be repeated in a song denoted by (the total number of sections - the number of available patterns) int numReps = totalSections - numpatterns; //if the random value says to utilize fewer sections than have been generated, simply make the song the list of all generated sections if (numReps < 1) { for (int i = 0; i < numpatterns; i++) { output.addSegment(patterns[i]); } } //otherwise make numReps number of repetitions in the production of the song else { //denotes the number value of the furthest section placed into the song int patNum = -1; //denotes the number value of the preceding section placed into the song int prevSec = -1; for (int i = 0; i < totalSections; i++) { //if you can't repeat anymore fill out the list if (numReps < 1) { patNum++; output.addSegment(patterns[patNum]); prevSec = patNum; } // if you can repeat else { //and you've already gone through the list, your only option is to repeat if (patNum + 1 == numpatterns) { do { randOutput = randomizer.Next(numpatterns); } while (randOutput == prevSec); numReps--; output.addSegment(patterns[randOutput]); prevSec = randOutput; } //if you haven't gotten all the way through the list, you can keep traversing or repeat else { randOutput = randomizer.Next(2); if (prevSec == 0 || randOutput == 0) { patNum++; output.addSegment(patterns[patNum]); prevSec = patNum; } else { do { randOutput = randomizer.Next(patNum + 1); } while (randOutput == prevSec); numReps--; output.addSegment(patterns[randOutput]); prevSec = randOutput; } } } } } BlottoBeats.MidiOut.MidiOut outgoing = new BlottoBeats.MidiOut.MidiOut(); double songLen = outgoing.outputToMidi(output); return songLen; }
public double generate_TwelveTone(SongParameters paramets) { const int NUMTONEROWS = 2; Random randomizer = new Random(paramets.seed); String gen; Song.SongSegment thisSection = new Song.SongSegment(); String[,] toneRows = new String[NUMTONEROWS,12]; String timeSigPattern = ""; //Simple or Compound Meter int timeSigQuant = 0; // 2 = Duple, 3 = Triple, etc String[] notes = { "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" }; //Set genre gen = paramets.genre; //Now also sets the genre Song output = new Song(paramets.tempo, "A", paramets.genre); Console.Out.WriteLine(paramets.genre); int randOutput = randomizer.Next(2); switch (randOutput) { case 0: timeSigPattern = "Simple"; break; case 1: timeSigPattern = "Compound"; break; } timeSigQuant = randomizer.Next(3) + 2; //Randomize toneRows for (int i = 0; i < NUMTONEROWS; i++) { int[] selected = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (int j = 0; j<12; j++){ int pos=0; do{ pos = randomizer.Next(12); }while(selected[pos]==1); selected[pos] = 1; toneRows[i, j] = notes[pos]; } } //Select quantity of phrases (8-32) int phrases = randomizer.Next(25) + 8; //Make the actual twelve-tone composition for (int i = 0; i < NUMTONEROWS; i++) { thisSection.melodies.Add(new Song.Melody()); for (int j = 0; j < phrases; j++) { int rhythmSum = 0; //Define measureLen int measureLen=0; if (timeSigPattern.Equals("Simple")) { measureLen += 4; } else measureLen += 6; measureLen *= timeSigQuant; int prevNoteVal = 0; int farthestPos = 0; while (rhythmSum < measureLen * 4) { int noteVal; int noteRhythm=0; int remainderOfMeasure = measureLen - (rhythmSum % measureLen); int measure = (rhythmSum / measureLen) + 1; int measureBound = 0; if (measure == 1) { measureBound = 2; } if (measure == 2) { measureBound = 4; } if (measure == 3) { measureBound = 6; } if (measure == 4) { measureBound = 12; } if (prevNoteVal == 0) { noteVal = prevNoteVal + 1; } else if (remainderOfMeasure - (measureBound - prevNoteVal) <= 1) { if (remainderOfMeasure - (measureBound - prevNoteVal) == 0) { noteVal = prevNoteVal + 1; } else { int randOut = randomizer.Next(2); noteVal = prevNoteVal + randOut; } } else { if (farthestPos == prevNoteVal) { int randOut = randomizer.Next(3) - 1; noteVal = prevNoteVal + randOut; } else { int randOut = randomizer.Next(2); noteVal = prevNoteVal + randOut; } } if (measure == 1) { if (noteVal > 4) noteVal = 4; } if (measure == 2) { if (noteVal > 9) noteVal = 9; } if (measure == 3) { if (noteVal > 11) noteVal = 11; } if (noteVal > 12) noteVal = 12; if (noteVal < 1) noteVal = 1; int maxlen = Math.Min((remainderOfMeasure - (measureBound - noteVal)), remainderOfMeasure); noteRhythm = randomizer.Next(maxlen) + 1; String noteString = toneRows[i, noteVal-1]; if (i == 0) { noteString += "5"; } if (i == 1) { noteString += "3"; } thisSection.melodies[i].melodicLine.Add(new Song.Note(noteString, noteRhythm)); rhythmSum += noteRhythm; prevNoteVal = noteVal; if (farthestPos < noteVal) { farthestPos = noteVal; } } } } output.addSegment(thisSection); BlottoBeats.MidiOut.MidiOut outgoing = new BlottoBeats.MidiOut.MidiOut(); double songLen = outgoing.outputToMidi(output); return songLen; }
private void insertData(SongParameters song) { MySqlConnection conn = new MySqlConnection(connString); SQLNonQuery(conn, "Insert into uploadedsongs (iduploadedsongs,genre,songseed,tempo,voteScore, idusers) values('" + song.ID + "','" + song.genre + "','" + song.seed + "','" + song.tempo + "','" + song.score + "','" + song.userID + "')"); }
public double generate_4Chord(SongParameters paramets) { Random randomizer = new Random(paramets.seed); int mode = 0; // 0 = Major 1 = Minor String key; String gen; Song.SongSegment[] thisSection = new Song.SongSegment[3]; String timeSigPattern = ""; //Simple or Compound Meter int timeSigQuant = 0; // 2 = Duple, 3 = Triple, etc String[] notes = { "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" }; //Select Key key = notes[randomizer.Next(12)]; //Now also sets the genre Song output = new Song(paramets.tempo, key, paramets.genre); Console.Out.WriteLine(paramets.genre); int randOutput = randomizer.Next(2); switch (randOutput) { case 0: timeSigPattern = "Simple"; break; case 1: timeSigPattern = "Compound"; break; } timeSigQuant = randomizer.Next(3) + 2; for (int i = 0; i < 3; i++) { if (i != 2) thisSection[i] = new Song.SongSegment(); else thisSection[i] = new Song.SongSegment(thisSection[0].chordPattern, thisSection[0].melodies, thisSection[0].melodies[0]); String chordProg=""; int subdiv=0; int measureLen = 0; if (timeSigPattern.Equals("Simple")) { measureLen += 4; } else measureLen += 6; measureLen *= timeSigQuant; if (timeSigQuant == 2) { randOutput = randomizer.Next(2) + 1; subdiv = measureLen / randOutput; } else if (timeSigQuant == 3) { randOutput = randomizer.Next(2) + 1; if (randOutput == 2) randOutput = 3; subdiv = measureLen / randOutput; } else { randOutput = randomizer.Next(3) + 1; if (randOutput == 3) randOutput = 4; subdiv = measureLen / randOutput; } int repPerMeasure = measureLen / subdiv; int randout = randomizer.Next(5); switch (randout) { case 0: chordProg = "6415"; break; case 1: chordProg = "1564"; break; case 2: chordProg = "1264"; break; case 3: chordProg = "1254"; break; case 4: chordProg = "4156"; break; } //Write chord progression if (i != 2) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { for (int l = 0; l < repPerMeasure; l++) { thisSection[i].chordPattern.Add(generateChord(mode, key, chordProg[k], subdiv)); } } } } //Write voice line composeMelody(thisSection[i], randomizer, key, mode, timeSigPattern, timeSigQuant); //Write bass line if (i != 2) { Song.Melody thisMelody = new Song.Melody(); for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { for (int l = 0; l < repPerMeasure; l++) { thisMelody.melodicLine.Add(thisSection[i].chordPattern[(j*4*repPerMeasure)+(k*repPerMeasure)+l].chordVoice.First()); } } } thisSection[i].melodies.Add(thisMelody); } //Fix ordering of voice and bass line after insertion in bridge if (i == 2) { Song.Melody tmp = thisSection[i].melodies[0]; thisSection[i].melodies[0] = thisSection[i].melodies[1]; thisSection[i].melodies[1] = tmp; } } output.addSegment(thisSection[0]); output.addSegment(thisSection[1]); output.addSegment(thisSection[0]); output.addSegment(thisSection[1]); output.addSegment(thisSection[2]); output.addSegment(thisSection[1]); BlottoBeats.MidiOut.MidiOut outgoing = new BlottoBeats.MidiOut.MidiOut(); double songLen = outgoing.outputToMidi(output); return songLen; }
/// <summary> /// Returns the given song with updated ID, score and user data. /// Does not add the song to the database if it isn't already there. /// </summary> /// <param name="song">Song to refresh</param> /// <returns>A SongParameters object that represents the song</returns> internal SongParameters RefreshSong(SongParameters song) { if (song.ID == -1) song.ID = GetID(song); // Song has no ID. Search the server if (song.ID == -1) { // Song is not in the database. Return score of 0 and userID of -1. song.score = 0; song.userID = -1; } else { object score = returnItem(song.ID, "voteScore", "uploadedsongs"); if (score != null && score is int) song.score = (int)score; else throw new DatabaseException("DATABASE ERROR: Invalid data type returned"); object userID = returnItem(song.ID, "idusers", "uploadedsongs"); if (userID != null && userID is int) song.userID = (int)userID; else throw new DatabaseException("DATABASE ERROR: Invalid data type returned"); } return song; }
/// <summary> /// Checks to see if a song is in the database already. If it isn't, the song is /// added to the database with a single vote. If it is, add the vote to that song. /// </summary> /// <param name="song">The song object to vote on</param> /// <param name="vote">The vote. True if upvote, false if downvote.</param> /// <param name="userID">The ID of the user who voted</param> internal SongParameters VoteOnSong(SongParameters song, bool vote, int userID) { if (song.ID == -1) song.ID = GetID(song); // Song has no ID. Search the server if (song.ID == -1) { // Song is not in the database. Insert it into the database. song.ID = GetNextAvailableID("uploadedsongs"); song.score = (vote) ? 1 : -1; song.userID = userID; insertData(song); addVoteToUserTable(userID, song.ID, vote); } else { int voteAmount = checkAndChangeVote(userID, song.ID, vote); updateScore(song.ID, voteAmount); object score = returnItem(song.ID, "voteScore", "uploadedsongs"); if (score != null && score is int) song.score = (int)score; else throw new DatabaseException("DATABASE ERROR: Invalid data type returned"); } return song; }
/// <summary> /// Sends an upload request with a single song and either an upvote or a downvote. /// The server will check the database for that song. If the song exists, it will /// add the vote to it, otherwise it will add the song to the server and save it /// with the vote. /// /// The response will contain a single SongParameters item with the song and it's score /// </summary> /// <param name="song">Song to upload</param> /// <param name="upOrDownvote">Vote. True if an upvote, false otherwise.</param> /// <param name="userInfo">The user authentication token</param> public BBRequest(SongParameters song, bool upOrDownvote, UserToken userInfo) { requestType = new UpDownVote(song, upOrDownvote, userInfo); }
/// <summary> /// Searches the database for a song that matches the given song. /// If there is a match, returns the ID. If not, returns -1 /// </summary> /// <param name="song">Song to search the database for</param> /// <returns>ID of the song on the server</returns> internal int GetID(SongParameters song) { MySqlConnection conn = new MySqlConnection(connString); MySqlCommand command = conn.CreateCommand(); command.CommandText = "Select iduploadedsongs from uploadedsongs where genre like '%" + song.genre + "%' and songseed like '%" + song.seed + "%' and tempo like '%" + song.tempo + "%'"; int returnId = -1; try { conn.Open(); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { returnId = (int)reader["iduploadedsongs"]; } } catch (MySqlException ex) { throw new DatabaseException("SQL Exception: " + ex.Message, ex); // Propagate the exception upwards after handling the finally block } finally { conn.Close(); } return returnId; }
public BBResponse(SongParameters song) { responseType = new SingleSong(song); }
public SingleSong(SongParameters song) { this.song = song; }
public UpDownVote(SongParameters song, bool vote, UserToken userInfo) : base(userInfo) { this.song = song; this.vote = vote; }
public RequestScore(SongParameters song, UserToken userInfo) : base(userInfo) { this.song = song; }
/// <summary> /// Sends a request for the score of a single song. /// /// The response will contain a single SongParameters item with the song and it's score /// </summary> /// <param name="song">Song to check the score of</param> /// <param name="userInfo">The user authentication token</param> public BBRequest(SongParameters song, UserToken userInfo) { requestType = new RequestScore(song, userInfo); }
/// <summary> /// Starts up the server /// </summary> public static void Main() { // Create a new server that listens on port 3000; //Database database = new Database("Server=localhost;Port=3306;Database=songdatabase;Uid=root;password=joeswanson;"); //Database database = new Database("Server=68.234.183.70;Port=3001;Database=songdatabase;Uid=BlottoServer;password=JJLrDtcrfvjym8gh1zUVklF19KDf1CTM;"); //Database database = new Database("Server=BlottoBeats.db.11772669.hostedresource.com;Port=3306;Database=BlottoBeats;Uid=BlottoBeats;password=JoeSwanson307!;"); //Server server = new Server(3000, database, "server.log", 3); Database database = null; Server server = null; // Server startup screen CommandLine.WriteLine("---------------------------------------------"); CommandLine.WriteLine(" BlottoBeats Server v1.0 (Stopped)"); CommandLine.WriteLine("---------------------------------------------"); CommandLine.WriteLine("Host ID: " + Properties.Settings.Default.hostID); CommandLine.WriteLine("Port: " + Properties.Settings.Default.port); CommandLine.WriteLine(); CommandLine.WriteLine("Database Name: " + Properties.Settings.Default.databaseName); CommandLine.WriteLine(); CommandLine.WriteLine("Username: "******"---------------------------------------------"); CommandLine.WriteLine("Type upstart <password> to log into the database and start the server."); CommandLine.WriteLine("Type help or ? for more commands."); while (true) { CommandLine line = CommandLine.Prompt(); switch (line.command.ToLower()) { // SERVER COMMANDS case "start": if (server != null && server.IsAlive()) CommandLine.WriteLine("ERROR: Can't start the server, the server is already started"); else if (database == null || server == null) CommandLine.WriteLine("ERROR: The update command needs to be run first"); else server.Start(); break; case "stop": if (server == null || !server.IsAlive()) CommandLine.WriteLine("ERROR: Can't stop the server, the server is already stopped"); else server.Stop(); break; case "restart": if (server == null || !server.IsAlive()) CommandLine.WriteLine("ERROR: Can't restart the server, the server is stopped"); else server.Restart(); break; case "update": case "updatedb": case "updatedatabase": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else if (server != null && server.IsAlive()) { CommandLine.WriteLine("ERROR: Can't update the database if the server is running. Stop the server first."); } else { database = new Database(Properties.Settings.Default.hostID, Properties.Settings.Default.port, Properties.Settings.Default.databaseName, Properties.Settings.Default.userID, line.args[0]); server = new Server(3000, database, "server.log", 3); CommandLine.WriteLine("Database updated successfully."); } break; case "upstart": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else if (server != null && server.IsAlive()) { CommandLine.WriteLine("ERROR: Can't update the database if the server is running. Stop the server first."); } else { database = new Database(Properties.Settings.Default.hostID, Properties.Settings.Default.port, Properties.Settings.Default.databaseName, Properties.Settings.Default.userID, line.args[0]); server = new Server(3000, database, "server.log", 3); CommandLine.WriteLine("Database updated successfully."); server.Start(); } break; // DATABASE COMMANDS case "dbhost": case "dbhostid": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else { Properties.Settings.Default.hostID = line.args[0]; Properties.Settings.Default.Save(); CommandLine.WriteLine("Setting is updated. You need to run the update command in order for the changes to be applied to the server."); } break; case "dbport": int port; if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else if (int.TryParse(line.args[0], out port) && port > 1024 && port <= 65535) { Properties.Settings.Default.port = port; Properties.Settings.Default.Save(); CommandLine.WriteLine("Setting is updated. You need to run the update command in order for the changes to be applied to the server."); } else { CommandLine.WriteLine("ERROR: The argument must be an integer between 1024 and 65535"); } break; case "database": case "databasename": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else { Properties.Settings.Default.databaseName = line.args[0]; Properties.Settings.Default.Save(); CommandLine.WriteLine("Setting is updated. You need to run the update command in order for the changes to be applied to the server."); } break; case "dbuser": case "dbuserid": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else { Properties.Settings.Default.userID = line.args[0]; Properties.Settings.Default.Save(); CommandLine.WriteLine("Setting is updated. You need to run the update command in order for the changes to be applied to the server."); } break; case "info": case "dbinfo": string health = "Stopped"; if (server != null && server.IsAlive()) health = "Running"; CommandLine.WriteLine("---------------------------------------------"); CommandLine.WriteLine(" BlottoBeats Server v1.0 (" + health + ")"); CommandLine.WriteLine("---------------------------------------------"); CommandLine.WriteLine("Host ID: " + Properties.Settings.Default.hostID); CommandLine.WriteLine("Port: " + Properties.Settings.Default.port); CommandLine.WriteLine(); CommandLine.WriteLine("Database Name: " + Properties.Settings.Default.databaseName); CommandLine.WriteLine(); CommandLine.WriteLine("Username: "******"---------------------------------------------"); break; // USER ACCOUNT COMMANDS case "newaccount": if (line.numArgs < 2) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires two arguments"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { UserToken token = database.Authenticate(new Credentials(line.args[0], line.args[1]), true); if (token != null) CommandLine.WriteLine("Registration Successful"); else CommandLine.WriteLine("Registration Failed"); } catch (DatabaseException ex) { CommandLine.WriteLine("DATABASE ERROR: Registration could not proceed"); CommandLine.WriteLine(ex.Message); } } break; case "deleteaccount": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { int id = database.GetID(line.args[0]); if (id != 0) { database.deleteUser(id); CommandLine.WriteLine("User '" + line.args[0] + "' deleted."); } else { CommandLine.WriteLine("ERROR:User '" + line.args[0] + "' does not exist"); } } catch (DatabaseException ex) { CommandLine.WriteLine("DATABASE ERROR: User could not be deleted"); CommandLine.WriteLine(ex.Message); } } break; case "resetpassword": if (line.numArgs < 2) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires two arguments"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { Credentials credentials = new Credentials(line.args[0], line.args[1]); int id = database.GetID(credentials.username); if (id != 0) { database.changePassword(id, credentials.GenerateHash()); CommandLine.WriteLine("Password Change Successful"); } else { CommandLine.WriteLine("ERROR:User '" + credentials.username + "' does not exist"); } } catch (DatabaseException ex){ CommandLine.WriteLine("DATABASE ERROR: Password could not be changed"); CommandLine.WriteLine(ex.Message); } } break; case "refreshtoken": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { if (database.RefreshToken(line.args[0])) CommandLine.WriteLine("Refreshed token"); else CommandLine.WriteLine("User '" + line.args[0] + "' does not exist"); } catch (DatabaseException ex) { CommandLine.WriteLine("DATABASE ERROR: Refresh could not proceed"); CommandLine.WriteLine(ex.Message); } } break; case "whois": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { int id; if (int.TryParse(line.args[0], out id)) { string name = database.GetUsername(id); if (name != null) CommandLine.WriteLine("Username of user " + id + " is '" + name + "'"); else CommandLine.WriteLine("User " + id + " does not exist"); } else { CommandLine.WriteLine("ERROR: Argument 1 must be an integer"); } } catch (DatabaseException ex) { CommandLine.WriteLine("DATABASE ERROR: Refresh could not proceed"); CommandLine.WriteLine(ex.Message); } } break; // SONG COMMANDS case "newsong": if (line.numArgs < 3) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires three arguments"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { int seed, tempo; if (line.numArgs < 4) { if (int.TryParse(line.args[0], out seed) && int.TryParse(line.args[1], out tempo)) { SongParameters song = new SongParameters(seed, tempo, line.args[2]); song = database.VoteOnSong(song, true, -1); CommandLine.WriteLine("Created new song with ID '" + song.ID + "' as anonymous"); } else { CommandLine.WriteLine("Arguments 1 and 2 must be integers"); } } else { int userID; if (int.TryParse(line.args[0], out seed) && int.TryParse(line.args[1], out tempo) && int.TryParse(line.args[3], out userID)) { SongParameters song = new SongParameters(seed, tempo, line.args[2], userID); song = database.VoteOnSong(song, true, -1); CommandLine.WriteLine("Created new song with ID '" + song.ID + "' belonging to user " + userID); } else { CommandLine.WriteLine("Arguments 1, 2, and 4 must be integers"); } } } catch (DatabaseException ex) { CommandLine.WriteLine("DATABASE ERROR: Could not create song"); CommandLine.WriteLine(ex.Message); } } break; case "deletesong": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { int id; if (int.TryParse(line.args[0], out id) && database.SongExists(id)) { database.deleteSong(id); CommandLine.WriteLine("Song '" + id + "' deleted."); } else { CommandLine.WriteLine("ERROR:Song '" + id + "' does not exist"); } } catch (DatabaseException ex) { CommandLine.WriteLine("DATABASE ERROR: Song could not be deleted"); CommandLine.WriteLine(ex.Message); } } break; case "setscore": if (line.numArgs < 2) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires two arguments"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { int id, score; if (int.TryParse(line.args[0], out id) && int.TryParse(line.args[1], out score) && database.SongExists(id)) { database.changeVoteScore(id, score); CommandLine.WriteLine("Song " + id + " score set to " + score); } else { CommandLine.WriteLine("ERROR:Song " + id + " does not exist"); } } catch (DatabaseException ex) { CommandLine.WriteLine("DATABASE ERROR: Score could not be changed"); CommandLine.WriteLine(ex.Message); } } break; case "songinfo": if (line.numArgs < 1) { CommandLine.WriteLine("ERROR: The command '" + line.command + "' requires an argument"); } else if (server == null || !server.IsAlive()) { CommandLine.WriteLine("ERROR: The server is offline"); } else { try { int id; if (int.TryParse(line.args[0], out id)) { if (database.SongExists(id)) { SongParameters song = database.GetSong(id); CommandLine.WriteLine("Song " + song.ID); CommandLine.WriteLine(); CommandLine.WriteLine("Seed: " + song.seed); CommandLine.WriteLine("Tempo: " + song.tempo); CommandLine.WriteLine("Genre: " + song.genre); CommandLine.WriteLine(); CommandLine.WriteLine("Score: " + song.score); CommandLine.WriteLine("User: "******"Song " + id + " does not exist"); } } else { CommandLine.WriteLine("ERROR: Argument 1 must be an integer"); } } catch (DatabaseException ex) { CommandLine.WriteLine("DATABASE ERROR: Could not get song info"); CommandLine.WriteLine(ex.Message); } } break; // Shell commands case "quit": case "exit": if (server != null && server.IsAlive()) server.Stop(); return; case "help": case "?": CommandLine.WriteLine("-------------------------"); CommandLine.WriteLine(" COMMAND LIST"); CommandLine.WriteLine("-------------------------"); CommandLine.WriteLine("SERVER COMMANDS"); CommandLine.WriteLine("Start"); CommandLine.WriteLine(" Starts the server"); CommandLine.WriteLine("Stop"); CommandLine.WriteLine(" Stops the server"); CommandLine.WriteLine("Restart"); CommandLine.WriteLine(" Restarts the server"); CommandLine.WriteLine("Update <password>"); CommandLine.WriteLine(" Modifies the database according to the changes. The server must be stopped."); CommandLine.WriteLine("Upstart <password>"); CommandLine.WriteLine(" Modifies the database according to the changes and starts the server. The server must be stopped."); CommandLine.WriteLine(); CommandLine.WriteLine("DATABASE COMMANDS"); CommandLine.WriteLine("DBHost/DBHostID <new ID>"); CommandLine.WriteLine(" Changes the hostID of the database. Requires an update to take effect."); CommandLine.WriteLine("DBPort <new Port>"); CommandLine.WriteLine(" Changes the port of the database. Requires an update to take effect."); CommandLine.WriteLine("Database/DatabaseName <new name>"); CommandLine.WriteLine(" Changes the name of the database. Requires an update to take effect."); CommandLine.WriteLine("DBUser/DBUserID <new id>"); CommandLine.WriteLine(" Changes the userID to use with the database. Requires an update to take effect."); CommandLine.WriteLine("DBInfo"); CommandLine.WriteLine(" Displays information about the server."); CommandLine.WriteLine(); CommandLine.WriteLine("USER ACCOUNT COMMANDS"); CommandLine.WriteLine("Newaccount <username> <password>"); CommandLine.WriteLine(" Creates a new user account with the given username and password."); CommandLine.WriteLine("Deleteaccount <username>"); CommandLine.WriteLine(" Deletes a user account with the given username."); CommandLine.WriteLine("Resetpassword <username> <password>"); CommandLine.WriteLine(" Resets the password of the given username to the given password."); CommandLine.WriteLine("Refreshtoken <username>"); CommandLine.WriteLine(" Refreshes the token associated with the given user account."); CommandLine.WriteLine("Whois <id>"); CommandLine.WriteLine(" Returns the username of the user with the given ID."); CommandLine.WriteLine(); CommandLine.WriteLine("SONG COMMANDS"); CommandLine.WriteLine("Newsong <seed> <tempo> <genre> [<userID>]"); CommandLine.WriteLine(" Adds a new song to the database with the given seed, tempo, and genre, and optionally, userID. Displays the ID of the newly-added song."); CommandLine.WriteLine("Deletesong <id>"); CommandLine.WriteLine(" Removes the given song from the database."); CommandLine.WriteLine("Setscore <id> <score>"); CommandLine.WriteLine(" Sets the score of a given song to the given score."); CommandLine.WriteLine("Songinfo <id>"); CommandLine.WriteLine(" Gets all the info about the given song."); break; default: CommandLine.WriteLine("'" + line.command + "' is not a valid command. Type help or ? for a list of commands."); break; } } }
private void sendScore(int tempScore, SongParameters tempSong, UserToken tempUser, int tempGenre) { if (server.Test()) { if (tempScore > 0) server.SendRequest(new BBRequest(tempSong, true, tempUser)); else if (tempScore < 0) server.SendRequest(new BBRequest(tempSong, false, tempUser)); createRedditThreads(); if (!this.IsDisposed) this.Invoke((MethodInvoker)delegate { this.Invalidate(); }); } }