Guitar
Guitar simulation using Karplus-Strong written in pure java. Playing some famous iranian songs by tablature!
Creates 6 thread for each GuitarString
whenever you start an instance of Guitar
, So if you play two note on two different strings, they won't block each other, meanwhile you can't play two notes on same string at the same time! Just like a real Guitar :)
Create an instance of Guitar:
Guitar guitar = new Guitar(8000, 1);
guitar.start();
Play Note:
guitar.playNote(GuitarNotes.STRINGS[string_index][fret_index]);
// OR
guitar.playNote(new Note(null, 98 /* freq */));
Play Chord:
guitar.playChordSubsequently(GuitarChords.Em);
// OR
guitar.playChordReverse(new Chord("Dm", 1, 3, 2, 0, -1, -1));
// OR
guitar.playChordSubsequentlyReverse(new Chord(3, "Gm", 0, 0, 0, 5, 5, 0));
Note: There are more interesting functions to customize playing a chord
Custom GuitarString
Create a custom string with its own thread
GuitarString string = new GuitarString(8000);
new GuitarStringPlayer(string, 1).start();
for (int i = 0; i < 6; i++) {
for (int j = 1; j <= 3; j++) {
Thread.sleep(200);
string.setPitch(GuitarNotes.STRINGS[i][j].getFrequency(), 0);
}
}
Test.mov
Play Tablature:
new Tablature()
.withListener((note, i) -> System.out.println(note))
.add(2, 0, 0)
.add(2, 3, 500)
.add(1, 3, 200)
.add(1, 1, 200)
.add(2, 3, 500)
...
.start(guitar);
Screen.Recording.2022-07-26.at.7.42.50.PM.mp4
BellaCiao.mov
See More:
- Bella Ciao
- Sarnevesht - Shadmehr Aghili
- Kooh - Shadmehr Aghili
- Do Panjereh - Googoosh
Android Implementation
Replace javax.sound.sampled
with android.media.AudioTrack
in GuitarStringPlayer
.
References
- Karplus-Strong Wikipedia
- Karplus-Strong algorithm Python
- Digital Synthesis of Plucked-String
- Guitar Frets & Their Notes vs Frequency
- Kooh-ShadmehrAghili Guitar Tab
- Bella Ciao Guitar Tab