Skip to content

Instantly share code, notes, and snippets.

@av1m
Created April 5, 2020 11:06
Show Gist options
  • Save av1m/984784f3366d3c96fb2a0d2a7a3864b5 to your computer and use it in GitHub Desktop.
Save av1m/984784f3366d3c96fb2a0d2a7a3864b5 to your computer and use it in GitHub Desktop.
Fibonacci-piano

Fibonacci Piano

Java project to create a piano sequence using the Fibonacci suite.
Piano notes are played using MIDI

Examples & Inspiration

  • An illustration video

IMAGE ALT TEXT HERE

  • Pseudo-code of Fibonacci suite, you can foud it here
// input: an integer n
// exit: the term of rank n of the Fibonacci sequence
fibo function (n)
     if n = 0 then return 0
     else if n = 1 then return 1
     else return fibo (n - 1) + fibo (n - 2)

Get started

  • Clone the project with the following command:
    git clone https://www.github.com/av1m/fibonacci-piano.git
    OR You an also see or download the project here

  • After that you can import the directory in your favorite IDE.
    Once it's done, all that remains is to contemplate the wonderful melody.

Author

Repository available on Github

License

License MIT

Copyright (c) 2019 AVI MIMOUN

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import javax.sound.midi.*;
/**
* @author [av1m](https://www.github.com/av1m)
* @author [Ilan Nizard](https://fr.linkedin.com/in/ilan-nizard-74a222140)
* <p>
* Thanks to [@wadael](https://github.com/wadael) for the initiation
* </p>
*/
public class Fibo {
/**
* Calculate the fibonacci sequence
*
* @param n
* @return long
*/
private static long fibonacci(final long n) {
return (1L >= n) ? n : Fibo.fibonacci(n - 1L) + Fibo.fibonacci(n - 2L);
}
/**
* Function that calls playMidiSong according to a note
*
* @param note
*/
public static int getTone(int note) {
switch (note) {
case 0:
case 7:
return 63; // Mi bemol
case 1:
case 8:
return 64; // Mi
case 3:
return 68; // Sol #
case 4:
return 57; // La
case 5:
return 59; // Si
case 6:
return 73; // Do #
case 9:
return 0;
default:
return 66; // Fa #
}
}
/**
* main
*
* @param args nothing expected
*/
public static void main(String[] args) throws MidiUnavailableException, InterruptedException {
final int NUMBER_ITERATION = 40;
final Synthesizer midiSynth = MidiSystem.getSynthesizer();
long fiboNumber = 0;
midiSynth.open();
// Get and load default instrument and schannel lists
final Instrument[] instr = midiSynth.getDefaultSoundbank().getInstruments();
final MidiChannel[] mChannels = midiSynth.getChannels();
midiSynth.loadInstrument(instr[0]); // Load an instrument
for (long i = 1; i <= NUMBER_ITERATION; i++) {
fiboNumber = Fibo.fibonacci(i);
System.out.println("== Fibo number : " + fiboNumber);
String notes = String.valueOf(fiboNumber);
for (int j = 0; j < notes.length(); j++) {
// Get size of string (long fibo) && transform each char to int
int oneSong = Character.getNumericValue(notes.charAt(j));
System.out.println("song Played : " + oneSong);
mChannels[0].noteOn(Fibo.getTone(oneSong), 100);
Thread.sleep(250L); // wait time in milliseconds to control duration
mChannels[0].noteOff((int) i); // Turn off the note
}
}
midiSynth.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment