Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key id/develop #2

Merged
merged 12 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions anothersong.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
What [Em]child is [G]this, who, [D]laid to rest
On [Em]Mary's [Am]lap, is [B]sleeping?
Whom [Em]angels [G]greet with [D]anthems sweet,
While [Em]shepherds [B]watch are [Esus]keep[Em]ing?
17 changes: 17 additions & 0 deletions asong.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# A simple ChordPro song.

{title: Swing Low Sweet Chariot}

{start_of_chorus}
Swing [Dsus4]low, sweet [G]chari[D]ot,
Comin' for to carry me [Aadd9]home.
Swing [D7]low, sweet [G/Bb]chari[D]ot,
Comin' for to [A7]carry me [D]home.
{end_of_chorus}

I [Dsus]looked over Jordan, and [G/B]what did I [D]see,
Comin' for to carry me [A/C#]home.
A [D]band of angels [G]comin' after [D]me,
Comin' for to [Aaug]carry me [Dm7]home.

{comment: Chorus}
211 changes: 207 additions & 4 deletions chordproconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
file.
"""

import os
import os.path
from os import path
import re

"""Definitions

Expand All @@ -20,9 +22,10 @@
file_name()
determine_direction()
determine_halfsteps()
open_file()
key_test()
new_song_file()
transposition_function()
key_test_final()

Variables
newsong
Expand All @@ -45,7 +48,7 @@ def file_name():
filename
"""
filename = input('What is the file name for the song you want to convert?')
if len(filename) < 1: filename = 'songeasy.txt' #For ease in testing
if len(filename) < 1: filename = 'song.txt' #For ease in testing
return filename
def determine_direction():
"""
Expand Down Expand Up @@ -136,7 +139,183 @@ def new_song_file():
+ '.txt', 'w+')
return newsongfile
break
def key_test_final():
"""
Currently in development.

This will identify the actual key of the song and return it.
This will prove useful in the future when transposing based on
key name and not on the number of halfsteps. This will make the program
more user friendly.

Development plans:
1. Create copy of original song with original key in name.
2. Write new song file with new key in its name.
3. Differentiate between minor and major keys.

functions
search_function(regex)
chord_counter(chords)
key()
minor_test()

variables
notelist
regexlist
item
chromaticnotes
\S+major
allkeyslist
minorkey

"""
notelist = dict() #Dictionary of what chords/notes are in the song
def search_function(regex):
"""
Searches song for regular expressions.

Searches the song for regular expressions that capture any
kind of chord and puts these chords into a dictionary.

variables
line
song
regx
stripped
fullstrip
"""
for line in song:
regx = re.search(regex, line)
if regx is None:
continue
else:
stripped = regx.group().strip('\[')
fullstrip = stripped.strip('\]')
notelist[fullstrip] = notelist.get(fullstrip, 0) + 1
regexlist = [
'\[A\S*?\]', '\[A#\S*?\]', '\[Bb\S*?\]', '\[B\S*?\]',
'\[C\S*?\]', '\[C#\S*?\]', '\[Db\S*?\]', '\[D\S*?\]',
'\[D#\S*?\]', '\[Eb\S*?\]', '\[E\S*?\]', '\[F\S*?\]',
'\[F#\S*?\]', '\[G\S*?\]', '\[G#\S*?\]', '\[Ab\S*?\]']
for item in regexlist:
search_function(item)

chromaticnotes = [
'A', 'A#', 'Bb', 'B',
'C', 'C#', 'Db', 'D',
'D#', 'Eb', 'E', 'F',
'F#', 'G', 'G#', 'Ab',
]
Amajor = ['A', 'Bm', 'C#m', 'D', 'E', 'F#m', 'G#dim']
Bbmajor = ['Bb', 'Cm', 'Dm', 'Eb', 'F', 'Gm', 'Adim']
Bmajor = ['B', 'C#m', 'D#m', 'E', 'F#', 'G#m', 'A#dim']
Cmajor = ['C', 'Dm', 'Em', 'F', 'G', 'Am', 'Bdim']
Dbmajor = ['Db', 'Ebm', 'Fm', 'Gb', 'Ab', 'Bbm', 'Cdim']
Dmajor = ['D', 'Em', 'F#m', 'G', 'A', 'Bm', 'C#dim']
Ebmajor = ['Eb', 'Fm', 'Gm', 'Ab', 'Bb', 'Cm', 'Ddim']
Emajor = ['E', 'F#m', 'G#m', 'A', 'B', 'C#m', 'D#dim']
Fmajor = ['F', 'Gm', 'Am', 'Bb', 'C', 'Dm', 'Edim']
Fsharpmajor = ['F#', 'G#m', 'A#m', 'B', 'C#', 'D#m', 'E#dim']
Gbmajor = ['Gb', 'Abm', 'Bbm', 'Cb', 'Db', 'Ebm', 'Fdim']
Gmajor = ['G', 'Am', 'Bm', 'C', 'D', 'Em', 'F#dim']
Abmajor = ['Ab', 'Bbm', 'Cm', 'Db', 'Eb', 'Fm', 'Gdim']
allkeyslist = [
Amajor, Bbmajor, Bmajor,
Cmajor, Dbmajor, Dmajor,
Ebmajor, Emajor, Fmajor,
Fsharpmajor, Gbmajor, Gmajor,
Abmajor]
allkeysnames = [
'Amajor', 'Bbmajor', 'Bmajor',
'Cmajor', 'Dbmajor', 'Dmajor',
'Ebmajor', 'Emajor', 'Fmajor',
'Fsharpmajor', 'Gbmajor', 'Gmajor',
'Abmajor']
def chord_counter(chords):
"""
Counts number of chords in a song that are in each 'chords' or key.
"""
times = 0
for key, val in notelist.items():
key = re.sub('7|sus4|sus|add9|/[A-Z][#b]|/[A-Z]', '', key)
for chord in chords:
if chord == key:
times += 1
else:
pass
return times
def minor_test():
"""
Tests if minor chords occur more.

This is a rough solution for whether or not the song should be in
the major key or the relative minor.

variables
bigkey
bigval
key
val
x
"""
bigkey = None
bigval = None
for key, val in notelist.items():
if bigval is None or val > bigval:
bigval = val
bigkey = key
x = re.search('\S+m', bigkey)
if x:
mkey = True
return mkey
def key():
"""
Finds the largest number of matching chords to a key and
returns the one (or two) with the highest count.

variables
counts
track
list
name
number
thekey
thecount
secondkey
key
val
"""
counts = 0
track = dict()
for list in allkeyslist:
name = allkeysnames[counts]
number = chord_counter(list)
track[name] = number
counts += 1
thekey = None
thecount = None
secondkey = None
for key, val in track.items():
if thecount is None or val > thecount:
thekey = key
thecount = val
for key, val in track.items():
if val == thecount:
secondkey = key
if secondkey == thekey:
secondkey = None
if secondkey is None:
print("The original song is in the key of " + thekey)
else:
print("The original song is in the key of "
+ thekey
+ " or "
+ secondkey)
if minorkey is True:
print("You may be in the relative minor of this major key.")

minorkey = minor_test()
key()

newsong = ''
majchordsharps = [
Expand Down Expand Up @@ -223,6 +402,18 @@ def new_song_file():
'[Ebsus4]', '[Esus4]', '[Fsus4]',
'[Gbsus4]', '[Gsus4]', '[Absus4]',
]
suschordsharps = [
'[Asus]', '[A#sus]', '[Bsus]',
'[Csus]', '[C#sus]', '[Dsus]',
'[D#sus]', '[Esus]', '[Fsus]',
'[F#sus]', '[Gsus]', '[G#sus]',
]
suschordflats = [
'[Asus]', '[Bbsus]', '[Bsus]',
'[Csus]', '[Dbsus]', '[Dsus]',
'[Ebsus]', '[Esus]', '[Fsus]',
'[Gbsus]', '[Gsus]', '[Absus]',
]
add9chordsharps = [
'[Aadd9]', '[A#add9]', '[Badd9]',
'[Cadd9]', '[C#add9]', '[Dadd9]',
Expand All @@ -235,15 +426,18 @@ def new_song_file():
'[Ebadd9]', '[Eadd9]', '[Fadd9]',
'[Gbadd9]', '[Gadd9]', '[Abadd9]',
]

fname = file_name()
updown = determine_direction()
halfsteps = determine_halfsteps()

songfile = open(fname, 'r+')
songraw = songfile.read()
song = songraw.split('\n')
songfile.close()

chordtype = key_test()
writesongfile = new_song_file()
key_test_final()

"""
This will reverse the lists of chords if the direction of transposition
Expand All @@ -264,6 +458,8 @@ def new_song_file():
diminishedchordsharps.reverse()
sus4chordflats.reverse()
sus4chordsharps.reverse()
suschordflats.reverse()
suschordsharps.reverse()
add9chordflats.reverse()
add9chordsharps.reverse()
else:
Expand Down Expand Up @@ -296,6 +492,7 @@ def transposition_function(chordlist):
count = 0
replaced = None
word = '[' + word
word = re.sub('/[A-Z][#b]|/[A-Z]', '', word) #Temp sol: X/Y chords
for item in chordlist:
if item in word and replaced is None:
newword = word.replace(item, chordlist[count - halfsteps])
Expand Down Expand Up @@ -332,6 +529,8 @@ def transposition_function(chordlist):
song = newsong.split('\n')
transposition_function(sus4chordflats)
song = newsong.split('\n')
transposition_function(suschordflats)
song = newsong.split('\n')
transposition_function(add9chordflats)
else:
transposition_function(majchordsharps)
Expand All @@ -348,7 +547,11 @@ def transposition_function(chordlist):
song = newsong.split('\n')
transposition_function(sus4chordsharps)
song = newsong.split('\n')
transposition_function(suschordsharps)
song = newsong.split('\n')
transposition_function(add9chordsharps)

writesongfile.write(newsong)
"""This section writes the new, transposed song file."""
writesongfile = new_song_file()
writesongfile.write(newsong.strip())
writesongfile.close()
17 changes: 0 additions & 17 deletions newsong.txt

This file was deleted.

17 changes: 0 additions & 17 deletions songTransposed.txt

This file was deleted.

29 changes: 0 additions & 29 deletions songTransposedTransposed.txt

This file was deleted.

Loading