Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Created October 18, 2018 18:28
Show Gist options
  • Save SethVandebrooke/3038343e544d6d14e98b928ed4c4c980 to your computer and use it in GitHub Desktop.
Save SethVandebrooke/3038343e544d6d14e98b928ed4c4c980 to your computer and use it in GitHub Desktop.
generate random basic chord progression
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Generate progression" HorizontalAlignment="Center" Margin="0,210,0,181" VerticalAlignment="Center" Width="136" RenderTransformOrigin="-0.006,0.236" Click="Button_Click" Height="29"/>
<Label Content="" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,70,0,0" Name="chordsTextBox" Height="59" Width="181" FontSize="22" VerticalAlignment="Top"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public Random rand = new Random();
private void Button_Click(object sender, RoutedEventArgs e)
{
List<string> chords = new List<string>();
int baseNote = rand.Next(12);
chords.Add(intToChord(baseNote));
chords.Add(intToChord(baseNote + 7));
chords.Add(intToChord(baseNote + 2));
chords.Add(intToChord(baseNote + 4 ) + "m");
List<string> progression = shuffleProgression(chords);
// Set the text in the text box to the progression
chordsTextBox.Content = String.Join(" ", progression.ToArray());
}
private string intToChord(int num)
{
List<string> chords = new List<string>() { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
if (num > 11)
{
while (num > 11)
{
num -= 11;
}
} else if (num < 0)
{
while (num < 0)
{
num += 12;
}
}
return chords[num];
}
private List<string> shuffleProgression(List<string> chordArray)
{
List<string> chords = new List<string>();
int index;
while (chordArray.Count >= 1)
{
index = rand.Next(chordArray.Count);
chords.Add(chordArray[index]);
chordArray.RemoveAt(index);
}
return chords;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment