Sammy1Am / Moppy2

The evolution of the Musical flOPPY controller
Other
311 stars 51 forks source link

Question on code #189

Closed Besteciler closed 1 year ago

Besteciler commented 1 year ago

Hello, such a question, there is a MoppyInstrument.h file, it contains arrays.

// Note period in microseconds const unsigned int notePeriods[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30578, 28861, 27242, 25713, 24270, 22909, 21622, 20409, 19263, 18182, 17161, 16198, //C1-B1 15289, 14436, 13621, 12856, 12135 , 11454, 10811, 10205, 9632, 9091, 8581, 8099, //C2 - B2 7645, 7218, 6811, 6428, 6068, 5727, 5406, 5103, 4816, 4546, 4291, 4050, //C3 - B3 3823, 3609, 3406, 3214, 3034, 2864, 2703, 2552, 2408, 2273, 2146, 2025, //C4 - B4 1911, 1804, 1703, 1607, 1517, 1432, 1351, 1276, 1204, 1136, 1073, 1012, //C5 - B5 956, 902, 851, 804, 758, 716, 676, 638, 602, 568, 536, 506, //C6 - B6 478, 451, 426, 402, 379, 358, 338, 319, 301, 284, 268, 253, //C7 - B7 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

Where did these numbers in the array come from and how do they work, as well as what is C1 - B1, C2 - B2 and so on. I would be grateful for an explanation

I'm not very good at programming

Sammy1Am commented 1 year ago

Hi there, a reasonable question.

Musical notes are given note names or letters, and these names repeat each octave. So to differentiate between different octaves, a number can be added to the note name. For example, A4 is an octave above A3.

Musical notes each have a certain pitch or frequency. For example, an A4 has a frequency of 440Hz (i.e. if it was a sine wave, the wave would repeat 440 times per second). Going up an octave represents a doubling of frequency, so an A5 has a frequency of 880Hz.

Floppy drives generate notes by stepping their stepper motor at the desired frequency, but in code this is implemented by taking a step, waiting a certain amount of time, and then taking another step. The amount of time between steps is call the period, and is the inverse of frequency (i.e. 1/frequency). So the period of an A4 is 1/440 or 0.0022727... seconds.

Many micro controllers allow you keep track of time in microseconds, so these periods are converted to microseconds and stored in that array to be looked up. Essentially the array allows us to take an incoming note like C3 and determine that the motor should be stepped every 7645 microseconds.