/// с числом вероятностей. Вероятность находится в отрезке [0, 100].
/// Сумма вероятностей должна быть равна 100</param>
public MyRandom(int[] values, int[] probabilities)
{
section = new int[100];
random = new Random();
if (values. Length!= probabilities. Length)
{
MessageBox. Show("Ошибка программы.\nЧисло объектов и вероятностей не совпадает!");
return;
}
int sum = 0;
for (int i = 0; i < probabilities. Length; i++)
{
sum += probabilities[i];
}
if (sum!= 100)
{
MessageBox. Show("Ошибка программы.\nСумма вероятностей не равна 100!");
}
int j = 0, k = 0;
for (int i = 0; i < values. Length; i++)
{
while (j < probabilities[i] && k < 100)
{
section[k] = values[i];
j++;
k++;
}
j = 0;
}
}
public int Next()
{
return section[random. Next(0, 100)];
}
public static int[] GetProbabilities(int[] values, ScaleName scaleName)
{
int[] probabilities = new int[values. Length];
/* По умолчанию сделаем равномерное распределение */
for (int i = 0; i < values. Length; i++)
{
values[i] = i;
probabilities[i] = 100 / probabilities. Length;
}
probabilities[0] += 100 - m();
if (scaleName!= ScaleName. Other && values. Length == 8)
{
probabilities = new int[] { 3, 40, 19, 19, 5, 5, 5, 4 };
}
if (scaleName == ScaleName. Flamenco && values. Length == 8)
{
probabilities = new int[] { 2, 75, 9, 3, 3, 3, 3, 2 };
}
if (scaleName == ScaleName. Flamenco2 && values. Length == 8)
{
probabilities = new int[] { 2, 75, 9, 3, 3, 3, 3, 2 };
}
if (scaleName == ScaleName. Blues && values. Length == 7)
{
probabilities = new int[] { 5, 40, 24, 19, 4, 4, 4 };
}
return probabilities;
}
}
Файл Parser. cs
public static class Parser
{
public static string GetString(int[] massive)
{
string s = "";
for (int i = 0; i < massive. Length; i++)
{
s += massive[i].ToString() + " ";
}
return s;
}
public static int[] GetMassive(string text)
{
int[] massive;
char[] separators = { ' ', '\r', '\n' };
string[] mass = text. Split(separators, StringSplitOptions. RemoveEmptyEntries);
massive = new int[mass. Length];
for (int i = 0; i < massive. Length; i++)
{
int res;
bool success = int. TryParse(mass[i], out res);
if (!success)
{
return null;
}
else
{
massive[i] = res;
}
}
if (massive. Length == 0)
return null;
return massive;
}
public static List<Melody> GetAllMelodys()
{
List<Melody> list = new List<Melody>();
StreamReader sr = new StreamReader("Melodys. txt", Encoding. Default);
String line = "";
while ((line = sr. ReadLine()) != null)
{
if (line == "")
continue;
char[] separators = { ';' };
string[] tokens = line. Split(separators, StringSplitOptions. RemoveEmptyEntries);
string name = tokens[0];
string scale = tokens[1];
string notesLine = tokens[2];
string rhythmLine = tokens[3];
ScaleName scaleName = MyScale. GetScaleName(scale);
char[] separators2 = { ' ' };
string[] stringNotes = notesLine. Split(separators2, StringSplitOptions. RemoveEmptyEntries);
string[] stringRhythm = rhythmLine. Split(separators2, StringSplitOptions. RemoveEmptyEntries);
int[] notes = new int[stringNotes. Length];
int[] rhythm = new int[stringRhythm. Length];
for (int i = 0; i < rhythm. Length; i++)
{
rhythm[i] = int. Parse(stringRhythm[i]);
}
for (int i = 0; i < notes. Length; i++)
{
notes[i] = int. Parse(stringNotes[i]);
}
Melody melody = new Melody(name, notes, rhythm, scaleName);
list. Add(melody);
}
sr. Close();
return list;
}
public static void SaveMelodyToFile(Melody melody)
{
string line = melody. Name + ";" + melody. ScaleName. ToString() + ";" + GetString(melody. Notes) + ";" + GetString(melody. Rhythm) + ";";
using (StreamWriter sw = new StreamWriter("Melodys. txt", true, Encoding. GetEncoding(1251)))
{
sw. WriteLine(line);
}
}
public static void RewriteMelodysFile(List<Melody> list)
{
StreamWriter sw = new StreamWriter("Melodys. txt", false, Encoding. GetEncoding(1251));
for (int i = 0; i < list. Count; i++)
{
string line = list[i].Name + ";" + list[i].ScaleName. ToString() + ";" + GetString(list[i].Notes) + ";" + GetString(list[i].Rhythm) + ";";
sw. WriteLine(line);
}
sw. Close();
}
}
Файл Accompanement. cs
public enum Pattern { Minor, Major }
public enum Direction { Down, Up }
public static class Accompaniment
{
public static void PlayAccord(Note root, ScaleName scaleName, Direction direction, OutputDevice outputDevice)
{
Pattern pattern;
if (scaleName == ScaleName. Minor || scaleName == ScaleName. Blues)
{
pattern = Pattern. Minor;
}
else
{
pattern = Pattern. Major;
}
root =(Note) (36 + ( (int)root % 12 ) );
if (pattern == Pattern. Minor)//тоника на 6 струне
{
if (direction == Direction. Down)
{
outputDevice. SendNoteOn(Channel. Channel1, root, 80);
Thread. Sleep(40);//20
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 7), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 3), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 7), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 12), 80);
}
else
{
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 12), 80);
Thread. Sleep(40);//20
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 7), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 3), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 7), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, root, 80);
}
}
else //Мажор, тоника на 6 струне
{
if (direction == Direction. Down)
{
outputDevice. SendNoteOn(Channel. Channel1, root, 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 7), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 4), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 7), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 24), 80);
}
else
{
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 24), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 7), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12 + 4), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 12), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, (Note)((int)root + 7), 80);
Thread. Sleep(40);
outputDevice. SendNoteOn(Channel. Channel1, root, 80);
}
}
}
Файл GUI_Methods
public partial class Form1 : Form
{
public static Label[,] labels;
public Button[,] buttons;
public Note[,] grifNotes;
public int[,] gridButtons;//кнопки, которые показывают сетку
public bool grid = false;
public bool editGenerated = false;
public bool editSaved = false;
public bool isGenerated = false;
public int[] minorScale, majorScale, flamencoScale, bluesScale, flamenco2Scale;
public MyScale selectedScale;
Note tonica;
Melody generatedMelody, savedMelody;
double duration = 6;
int notesCount = 16;
OutputDevice outputDevice;
public MediaPlayer player;
SoundDevices sd;
List<Melody> generatedMelodyList;
List<Melody> savedMelodyList;
private void Form1_Load(object sender, EventArgs e)
{
savedMelodyList = new List<Melody>();
savedMelodyList = Parser. GetAllMelodys();
for (int i = 0; i < savedMelodyList. Count; i++)
|
Из за большого объема этот материал размещен на нескольких страницах:
1 2 3 4 5 6 7 8 9 10 11 |


