Translate number pairs to guitar notes
JavaScript (ES6), 79 70 bytes
a=>a.map(([s,f])=>"AA#BCC#DD#EFF#GG#".match(/.#?/g)[(s*7+(s>2)+f)%12])
Requires 1-based strings. Edit: Saved 9 bytes by directly calculating the string to fret conversion, based on @nimi's old answer.
Mathematica, 62 bytes (non-competing)
<<Music`;MusicScale[100(#2+{24,19,15,10,5,0}[[#]])&@@@#,E2,9]&
The {24,19,15,10,5,0}
and the E2
represent the open-string tones of the six guitar strings (for example, the top string is 24 semitones above the note E2). Non-competing because it doesn't print the names of the notes—it plays the sequence of notes! (only if you have Mathematica, unfortunately) For example,
<<Music`;MusicScale[100(#2+{24,19,15,10,5,0}[[#]])&@@@#,E2,9]&@
{{4,0},{3,2},{2,3},{1,2},{5,0},{4,2},{3,2},{2,2},
{5,2},{4,4},{2,0},{2,3},{6,2},{4,4},{3,2},{2,2},
{6,3},{4,0},{3,0},{2,0},{4,0},{4,4},{3,2},{2,3},
{6,3},{3,0},{2,0},{2,3},{5,0},{4,2},{3,2},{2,2},{4,0}}
plays the opening 4 bars or so from Pachelbel's Canon. (which is about as much of Pachelbel's Canon as I can stand)
MATL, 48 47 45 bytes
Thanks to @Emigna for a correction regarding input format.
Guitar and code golf... I had to answer this one!
'$)-27<'i)-'F F# G G# A A# B C C# D D#
Input format is: an array of (1-based) string, then an array of (0-based) frets.
Try it online!
Explanation
Some language features used in this answer:
- A string is automatically converted to a numerical array of ASCII code points when some arithmetic operation is applied to it.
- Arithmetical operations work element-wise, i.e. vectorized. So the subtraction of a string and a numerical array of the same size gives an array with the subtraction of corresponding entries.
- Indexing is 1-based and modular.
- A cell array is like a list in other languages. It can contain arbitrary elements, possibly arrays of different types or sizes. Here a cell array will be used to store strings of different lengths (the notes' names).
Commented code:
'$)-27<' % Push this string
i % Take first input (array of guitar strings)
) % Index into the string. For example, input [1 3] gives
% the string '$-' (indexing is 1-based)
- % Implicitly take second input (array of guitar frets).
% Subtract element-wise. This automatically converts the
% previous string into an array of ASCII codes. For
% example, second input [1 5] gives a result [-35 -40],
% which is [1 5] minus [36 45], where 36 and 45 are the
% ASCII codes of '$-'
'F F# G G# A A# B C C# D D# E' % Push this string
Yb % Split at spaces. Gives a cell array of 12 (sub)strings:
% {'F', 'F#', 'G', ..., 'E'}
w) % Swap and index into the cell array of strings.
% Indexing is 1-based and modular. In the example, since
% the cell array has 12 elements, the indexing array
% [-35 -40] is the same [1 8], and thus it gives a
% (sub-)array formed by the first and eighth cells:
% {'F', 'C'}. This is displayed as the cells' contents,
% one per line