private void generateButton_Click(object sender, EventArgs e) { int[] notes = Notes.GetNotes(Notes.Chords.Am, 1); int[] rhythm = Rhythm.GetRhythm(6, 4); //testTextBox.Text = Parser.GetString(notes); player.Open(new Uri(Application.StartupPath + "\\Chords\\Am.m4a", UriKind.Absolute)); player.Play(); SoundDevices sd = new SoundDevices(outputDevice, Channel.Channel1); MelodyPlayer.PlayMelody(sd, notes, player); notes = Notes.GetNotes(Notes.Chords.F, 2); //testTextBox.Text = Parser.GetString(notes); MelodyPlayer.PlayMelody(sd, notes, player); notes = Notes.GetNotes(Notes.Chords.Dm, 2); //testTextBox.Text = Parser.GetString(notes); MelodyPlayer.PlayMelody(sd, notes, player); notes = Notes.GetNotes(Notes.Chords.E, 4); //testTextBox.Text = Parser.GetString(notes); MelodyPlayer.PlayMelody(sd, notes, player); }
private void playGeneratedMelodyButton_Click(object sender, EventArgs e) { SetEnable(false, SetEnableMode.All); isGenerated = true; if (generatedMelody != null) { if (sd == null) { sd = new SoundDevices(outputDevice, Channel.Channel1); } SetLabelsNameNotesAndRhythm(generatedMelody); //saveMelodyButton.Enabled = true; duration = Rhythm.GetDuration(tempoTrackBar.Value, generatedMelody.Notes.Length); cts = new CancellationTokenSource(); stopButton.Select(); timer.Start(); task = Task.Run(() => { MelodyPlayer.PlayMelodyWithRhythm(sd, generatedMelody, tonica, duration, grifNotes, buttons, cts.Token); }, cts.Token); } }
public static void PlayMelodyWithRhythm(SoundDevices sd, Melody melody, Note tonica, double duration, Note[,] grifNotes, Button[,] buttons, CancellationToken ct) { sd.output.SilenceAllNotes(); int[] notes = melody.Notes; int[] rhythm = melody.Rhythm; PlayMelodyWithRhythm(sd, notes, rhythm, tonica, duration, grifNotes, buttons, ct); }
private void playYourMelodyButton_Click(object sender, EventArgs e) { SetEnable(false, SetEnableMode.All); int[] notes = Parser.GetMassive(notesTextBox.Text); int[] rhythm = Parser.GetMassive(rhythmTextBox.Text); if (notes.Length == 0 || rhythm.Length == 0) { MessageBox.Show("Неверный формат мелодии!"); return; } notesCountTextBox.Text = notes.Length.ToString(); duration = Rhythm.GetDuration(tempoTrackBar.Value, notes.Length); if (sd == null) { sd = new SoundDevices(outputDevice, Channel.Channel1); } Melody melody = new Melody("My melody", notes, rhythm, ScaleName.Other); cts = new CancellationTokenSource(); stopButton.Select(); timer.Start(); task = Task.Run(() => { MelodyPlayer.PlayMelodyWithRhythm(sd, melody, tonica, duration, grifNotes, buttons, cts.Token); }, cts.Token); }
public static void PlayMelody(SoundDevices sd, int[] phrase, MediaPlayer player) { Thread.Sleep(700); for (int i = 0; i < phrase.Length; i++) { sd.output.SendNoteOn(sd.channel, MyNote.Note(phrase[i], 4), 80); System.Threading.Thread.Sleep(440); sd.output.SendNoteOff(sd.channel, MyNote.Note(phrase[i], 4), 80); } System.Threading.Thread.Sleep(500); Form1.Replay(player); }
private void newGenerateButton_Click(object sender, EventArgs e) { tabControl1.SelectedIndex = 0; duration = Rhythm.GetDuration(tempoTrackBar.Value, notesCount); Random random = new Random(); int divisor = random.Next(3, 5), addition = random.Next(1, 4); int[] rhythm = Rhythm.GetRhythm(notesCount + notesCount / divisor + addition, notesCount); int[] notes = Notes.GetNotes(selectedScale, notesCount, rhythm); generatedMelody = new Melody(Melody.Number + "-ая мелодия", notes, rhythm, selectedScale.scaleName); generatedMelodysList.Add(generatedMelody); generatedMelodysComboBox.Items.Add(generatedMelody.Name + "(" + generatedMelody.ScaleName.ToString() + ")"); generatedMelodysComboBox.SelectedIndex = generatedMelodysList.IndexOf(generatedMelody); Melody.Number++; rhythmTextBox.Text = Parser.GetString(rhythm); notesTextBox.Text = Parser.GetString(notes); if (sd == null) { sd = new SoundDevices(outputDevice, Channel.Channel1); } SetLabelsNameNotesAndRhythm(generatedMelody); //saveMelodyButton.Enabled = true; SetEnable(false, SetEnableMode.All); cts = new CancellationTokenSource(); stopButton.Select(); timer.Start(); task = Task.Run(() => { MelodyPlayer.PlayMelodyWithRhythm(sd, generatedMelody, tonica, duration, grifNotes, buttons, cts.Token); }, cts.Token); }
/// <summary> /// Проигрывает мелодию в указанном ритме от указанной ноты. /// С фиксированной продолжительностью такта. /// Играется столько нот, сколько звучащих /// нот в ритме(столько, сколько единиц в массиве ритма) /// </summary> /// <param name="sd">Aggregates OutputDevice and Channel</param> /// <param name="notes">Массив нот</param> /// <param name="rhythm">Массив, описывающий ритм</param> /// <param name="tonica">Нота, от которой играется мелодия</param> /// <param name="duration">Продолжительность такта в секундах с точностью до тысячных</param> public static void PlayMelodyWithRhythm(SoundDevices sd, int[] notes, int[] rhythm, Note tonica, double duration, Note[,] grifNotes, Button[,] buttons, CancellationToken ct) { /* Продолжительность такта в миллисекундах */ int dur = (int)(duration * 1000); int divisor = 0, k = 0, l = 0; while (k < notes.Length && l < rhythm.Length) { divisor++; if (rhythm[l] == 1) { k++; } l++; } /* Продолжительность звучания одной ноты(или паузы) */ int oneNoteDur = dur / divisor; Note note = Note.A0; int j = 0; for (int i = 0; i < rhythm.Length; i++) { if (ct.IsCancellationRequested) { return; } if (rhythm[i] == 0) { System.Threading.Thread.Sleep(oneNoteDur); } else { /* 40 - E2, * 79 - G5 */ int n = ((int)tonica - 1 + notes[j]); while (n > 79) { n = n - 12; } while (n < 40) { n = n + 12; } sd.output.SendNoteOff(sd.channel, note, 80); note = (Note)n; sd.output.SendNoteOn(sd.channel, note, 80); bool visible; Button button = FindButtonAndChangeColor(note, buttons, grifNotes, out visible); System.Threading.Thread.Sleep(oneNoteDur); button.BackColor = default(System.Drawing.Color); if (!visible && button.InvokeRequired) { button.Invoke(new Action(() => { button.Visible = false; })); } if (Form1.ActiveForm != null && Form1.ActiveForm.InvokeRequired) { Form1.ActiveForm.Invoke(new Action(() => { Form1.ActiveForm.Refresh(); })); } j++; if (j == notes.Length) { return; } } } sd.output.SendNoteOff(sd.channel, note, 80); }
private void playGeneratedMelodyButton_Click(object sender, EventArgs e) { SetEnable(false, SetEnableMode.All); isGenerated = true; if (generatedMelody != null) { if (sd == null) sd = new SoundDevices(outputDevice, Channel.Channel1); SetLabelsNameNotesAndRhythm(generatedMelody); //saveMelodyButton.Enabled = true; duration = Rhythm.GetDuration(tempoTrackBar.Value, generatedMelody.Notes.Length); cts = new CancellationTokenSource(); stopButton.Select(); timer.Start(); task = Task.Run(() => { MelodyPlayer.PlayMelodyWithRhythm(sd, generatedMelody, tonica, duration, grifNotes, buttons, cts.Token); }, cts.Token); } }
private void newGenerateButton_Click(object sender, EventArgs e) { tabControl1.SelectedIndex = 0; duration = Rhythm.GetDuration(tempoTrackBar.Value, notesCount); Random random = new Random(); int divisor = random.Next(3, 5), addition = random.Next(1, 4); int[] rhythm = Rhythm.GetRhythm(notesCount + notesCount / divisor + addition, notesCount); int[] notes = Notes.GetNotes(selectedScale, notesCount, rhythm); generatedMelody = new Melody(Melody.Number + "-ая мелодия", notes, rhythm, selectedScale.scaleName); generatedMelodysList.Add(generatedMelody); generatedMelodysComboBox.Items.Add(generatedMelody.Name + "(" + generatedMelody.ScaleName.ToString() + ")"); generatedMelodysComboBox.SelectedIndex = generatedMelodysList.IndexOf(generatedMelody); Melody.Number++; rhythmTextBox.Text = Parser.GetString(rhythm); notesTextBox.Text = Parser.GetString(notes); if (sd == null) sd = new SoundDevices(outputDevice, Channel.Channel1); SetLabelsNameNotesAndRhythm(generatedMelody); //saveMelodyButton.Enabled = true; SetEnable(false, SetEnableMode.All); cts = new CancellationTokenSource(); stopButton.Select(); timer.Start(); task = Task.Run(() => { MelodyPlayer.PlayMelodyWithRhythm(sd, generatedMelody, tonica, duration, grifNotes, buttons, cts.Token); }, cts.Token); }
private void playYourMelodyButton_Click(object sender, EventArgs e) { SetEnable(false, SetEnableMode.All); int[] notes = Parser.GetMassive(notesTextBox.Text); int[] rhythm = Parser.GetMassive(rhythmTextBox.Text); if (notes.Length == 0 || rhythm.Length == 0) { MessageBox.Show("Неверный формат мелодии!"); return; } notesCountTextBox.Text = notes.Length.ToString(); duration = Rhythm.GetDuration(tempoTrackBar.Value, notes.Length); if(sd == null) sd = new SoundDevices(outputDevice, Channel.Channel1); Melody melody = new Melody("My melody", notes, rhythm, ScaleName.Other); cts = new CancellationTokenSource(); stopButton.Select(); timer.Start(); task = Task.Run(() => { MelodyPlayer.PlayMelodyWithRhythm(sd, melody, tonica, duration, grifNotes, buttons, cts.Token); }, cts.Token); }
/// <summary> /// Проигрывает мелодию в указанном ритме от указанной ноты. /// С фиксированной продолжительностью такта. /// Играется столько нот, сколько звучащих /// нот в ритме(столько, сколько единиц в массиве ритма) /// </summary> /// <param name="sd">Aggregates OutputDevice and Channel</param> /// <param name="notes">Массив нот</param> /// <param name="rhythm">Массив, описывающий ритм</param> /// <param name="tonica">Нота, от которой играется мелодия</param> /// <param name="duration">Продолжительность такта в секундах с точностью до тысячных</param> public static void PlayMelodyWithRhythm(SoundDevices sd, int[] notes, int[] rhythm, Note tonica, double duration, Note[,] grifNotes, Button[,] buttons, CancellationToken ct) { /* Продолжительность такта в миллисекундах */ int dur = (int)(duration * 1000); int divisor = 0, k = 0, l = 0; while(k < notes.Length && l < rhythm.Length) { divisor++; if (rhythm[l] == 1) k++; l++; } /* Продолжительность звучания одной ноты(или паузы) */ int oneNoteDur = dur / divisor; Note note = Note.A0; int j = 0; for (int i = 0; i < rhythm.Length; i++) { if (ct.IsCancellationRequested) return; if (rhythm[i] == 0) { System.Threading.Thread.Sleep(oneNoteDur); } else { /* 40 - E2, * 79 - G5 */ int n = ((int)tonica - 1 + notes[j]); while (n > 79) n = n - 12; while (n < 40) n = n + 12; sd.output.SendNoteOff(sd.channel, note, 80); note = (Note)n; sd.output.SendNoteOn(sd.channel, note, 80); bool visible; Button button = FindButtonAndChangeColor(note, buttons, grifNotes, out visible); System.Threading.Thread.Sleep(oneNoteDur); button.BackColor = default(System.Drawing.Color); if (!visible && button.InvokeRequired) button.Invoke(new Action(() => { button.Visible = false; })); if (Form1.ActiveForm != null && Form1.ActiveForm.InvokeRequired) Form1.ActiveForm.Invoke(new Action(() => { Form1.ActiveForm.Refresh(); })); j++; if (j == notes.Length) return; } } sd.output.SendNoteOff(sd.channel, note, 80); }