9
1
Task
Write a program to determine the note sounded, along with how many cents out of tune, of a string tuned to a given frequency and pressed down at a given point.
For the sake of simplicity, assume that the frequency of the sound produced and the length of the string to the right of where it is pressed are inversely proportional.
Note: this task deals solely in the fundamental tone, and not with overtones/other harmonics.
Input
Your program is given two pieces of data:
A string of arbitrary length, representing the string in question. This string will be marked with an X where the string is to be held down.
[-----] is a string divided in six sections (five divisions). [--X--] is a string pressed at the exact center of the string. [X----] is a string pressed at 1/6 the length of the string. (Length used is 5/6) [-X--] is a string pressed at 2/5 of the length of the string. (Length used is 3/5)
Assume the note is sounded using the part of the string to the right of the
X
.- A number (not necessarily an integer), signifying the frequency at which the string is tuned. The precision of this number will be at most four digits past the decimal.
It may be assumed that frequencies passed will lie between 10 Hz
and 40000 Hz
.
Input may be passed in a format of your choice. Please specify how input is accepted into your program in your answer.
Output
Your program must output both the closest note* in the twelve-tone equal temperament tuning system, and the number of cents away from the closest note that the sound denoted by the string would be (rounded to the nearest cent).
+n
cents should be used to denote n
cents sharp/above the note, and -n
cents for flat/below the note.
The note should be outputted in scientific pitch notation. Assume A4 is tuned to 440Hz
. Use b and # for flat/sharp notes. Note: Either sharp or flat may be used. For the note at 466.16Hz
, either A#
or Bb
may be outputted for the note.
Format of output is up to you, as long as the output contains only the two previously specified pieces of information (i.e. printing every single possible output is not allowed).
*closest note refers to the note that is closest to the sound denoted by the input, measured in the number of cents (therefore, the note that is within 50 cents
of the sound). If the sound is 50 cents
away from two different notes (after rounding), then either of the two notes may be outputted.
Examples
Your program should work for all cases, not just the following examples.
Output Input Frequency Input String
A4, +0 cents 220 [-----X-----]
A5, +0 cents 220 [--------X--]
D5, -2 cents 440 [--X--------]
B4, -49 cents 440 [X----------]
A#4, +19 cents* 314.1592 [X-]
Eb9, +8 cents* 400 [-----------------------X]
Eb11,+8 cents* 100 [--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X]
D#1, +49 cents* 10 [--X]
A0, -11 cents 11.7103 [---X--]
*Either sharp or flat could have been outputted.
Potentially Helpful Links
This is code-golf so shortest answer wins.
I think your examples are somewhat inconsistent: According to the first one
[--X--]
the string is pressed in the middle of the division where thex
is placed, while the last one[-X--]
would be at 3/8 (not 2/5) when following this logic. Or do I understand something wrong? – flawr – 2014-08-16T20:57:14.823@flawr for the last one,
[-X--]
, the string is divided at 4 places (and therefore into 5 parts), and pressed at the second of these divisions. Thus, it is pressed at2/5
, and the length used is3/5
. – es1024 – 2014-08-16T21:25:09.397Ah ok now I see, so each
-
basically represents the position of the divisions, thank you for explaining! – flawr – 2014-08-16T21:34:37.237