Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: utf8 hash keys revisited: How can i use musical symbols as hash keys?

by Jim (Curate)
on Oct 06, 2014 at 23:08 UTC ( [id://1103032]=note: print w/replies, xml ) Need Help??


in reply to utf8 hash keys revisited: How can i use musical symbols as hash keys?

Consider this alternative, more extensible way of handling enharmonic notes.

use v5.14;
use strict;
use warnings;
use utf8;

binmode STDOUT, ':encoding(UTF-8)';

my %helmholtz_to_midi;

while (<DATA>) {
    chomp;

    my ($helmholtz_note_name, $midi_octave, $midi_note_number, $frequency)
        = split m/,/, $_, 4;

    $helmholtz_to_midi{$helmholtz_note_name} = {
        midi_octave      => $midi_octave,
        midi_note_number => $midi_note_number,
        frequency        => $frequency,
    };
}

say $helmholtz_to_midi{'C₂'}{midi_octave};      # Prints -4
say $helmholtz_to_midi{'c¹'}{midi_note_number}; # Prints 60
say $helmholtz_to_midi{'a¹'}{frequency};        # Prints 440.0000000000
say $helmholtz_to_midi{'a♯¹'}{frequency};       # Prints 466.1637615181
say $helmholtz_to_midi{'b♭¹'}{frequency};       # Prints 466.1637615181
say $helmholtz_to_midi{'g⁵'}{frequency};        # Prints 6271.9269757080

exit 0;

__DATA__
C₂,-4,12,16.3515978313
C♯₂,-4,13,17.3239144361
D♭₂,-4,13,17.3239144361
D₂,-4,14,18.3540479948
D♯₂,-4,15,19.4454364826
E♭₂,-4,15,19.4454364826
E₂,-4,16,20.6017223071
F₂,-4,17,21.8267644646
F♯₂,-4,18,23.1246514195
G♭₂,-4,18,23.1246514195
G₂,-4,19,24.4997147489
G♯₂,-4,20,25.9565435987
A♭₂,-4,20,25.9565435987
A₂,-4,21,27.5000000000
A♯₂,-4,22,29.1352350949
B♭₂,-4,22,29.1352350949
B₂,-4,23,30.8677063285
C₁,-3,24,32.7031956626
C♯₁,-3,25,34.6478288721
D♭₁,-3,25,34.6478288721
D₁,-3,26,36.7080959897
D♯₁,-3,27,38.8908729653
E♭₁,-3,27,38.8908729653
E₁,-3,28,41.2034446141
F₁,-3,29,43.6535289291
F♯₁,-3,30,46.2493028390
G♭₁,-3,30,46.2493028390
G₁,-3,31,48.9994294977
G♯₁,-3,32,51.9130871975
A♭₁,-3,32,51.9130871975
A₁,-3,33,55.0000000000
A♯₁,-3,34,58.2704701898
B♭₁,-3,34,58.2704701898
B₁,-3,35,61.7354126570
C,-2,36,65.4063913251
C♯,-2,37,69.2956577442
D♭,-2,37,69.2956577442
D,-2,38,73.4161919794
D♯,-2,39,77.7817459305
E♭,-2,39,77.7817459305
E,-2,40,82.4068892282
F,-2,41,87.3070578583
F♯,-2,42,92.4986056779
G♭,-2,42,92.4986056779
G,-2,43,97.9988589954
G♯,-2,44,103.8261743950
A♭,-2,44,103.8261743950
A,-2,45,110.0000000000
A♯,-2,46,116.5409403795
B♭,-2,46,116.5409403795
B,-2,47,123.4708253140
c,-1,48,130.8127826503
c♯,-1,49,138.5913154884
d♭,-1,49,138.5913154884
d,-1,50,146.8323839587
d♯,-1,51,155.5634918610
e♭,-1,51,155.5634918610
e,-1,52,164.8137784564
f,-1,53,174.6141157165
f♯,-1,54,184.9972113558
g♭,-1,54,184.9972113558
g,-1,55,195.9977179909
g♯,-1,56,207.6523487900
a♭,-1,56,207.6523487900
a,-1,57,220.0000000000
a♯,-1,58,233.0818807590
b♭,-1,58,233.0818807590
b,-1,59,246.9416506281
c¹,0,60,261.6255653006
c♯¹,0,61,277.1826309769
d♭¹,0,61,277.1826309769
d¹,0,62,293.6647679174
d♯¹,0,63,311.1269837221
e♭¹,0,63,311.1269837221
e¹,0,64,329.6275569129
f¹,0,65,349.2282314330
f♯¹,0,66,369.9944227116
g♭¹,0,66,369.9944227116
g¹,0,67,391.9954359817
g♯¹,0,68,415.3046975799
a♭¹,0,68,415.3046975799
a¹,0,69,440.0000000000
a♯¹,0,70,466.1637615181
b♭¹,0,70,466.1637615181
b¹,0,71,493.8833012561
c²,1,72,523.2511306012
c♯²,1,73,554.3652619537
d♭²,1,73,554.3652619537
d²,1,74,587.3295358348
d♯²,1,75,622.2539674442
e♭²,1,75,622.2539674442
e²,1,76,659.2551138257
f²,1,77,698.4564628660
f♯²,1,78,739.9888454233
g♭²,1,78,739.9888454233
g²,1,79,783.9908719635
g♯²,1,80,830.6093951599
a♭²,1,80,830.6093951599
a²,1,81,880.0000000000
a♯²,1,82,932.3275230362
b♭²,1,82,932.3275230362
b²,1,83,987.7666025122
c³,2,84,1046.5022612024
c♯³,2,85,1108.7305239075
d♭³,2,85,1108.7305239075
d³,2,86,1174.6590716696
d♯³,2,87,1244.5079348883
e♭³,2,87,1244.5079348883
e³,2,88,1318.5102276515
f³,2,89,1396.9129257320
f♯³,2,90,1479.9776908465
g♭³,2,90,1479.9776908465
g³,2,91,1567.9817439270
g♯³,2,92,1661.2187903198
a♭³,2,92,1661.2187903198
a³,2,93,1760.0000000000
a♯³,2,94,1864.6550460724
b♭³,2,94,1864.6550460724
b³,2,95,1975.5332050245
c⁴,3,96,2093.0045224048
c♯⁴,3,97,2217.4610478150
d♭⁴,3,97,2217.4610478150
d⁴,3,98,2349.3181433393
d♯⁴,3,99,2489.0158697766
e♭⁴,3,99,2489.0158697766
e⁴,3,100,2637.0204553030
f⁴,3,101,2793.8258514640
f♯⁴,3,102,2959.9553816931
g♭⁴,3,102,2959.9553816931
g⁴,3,103,3135.9634878540
g♯⁴,3,104,3322.4375806396
a♭⁴,3,104,3322.4375806396
a⁴,3,105,3520.0000000000
a♯⁴,3,106,3729.3100921447
b♭⁴,3,106,3729.3100921447
b⁴,3,107,3951.0664100490
c⁵,4,108,4186.0090448096
c♯⁵,4,109,4434.9220956300
d♭⁵,4,109,4434.9220956300
d⁵,4,110,4698.6362866785
d♯⁵,4,111,4978.0317395533
e♭⁵,4,111,4978.0317395533
e⁵,4,112,5274.0409106059
,4,113,5587.6517029281
f♯⁵,4,114,5919.9107633862
g♭⁵,4,114,5919.9107633862
g⁵,4,115,6271.9269757080
g♯⁵,4,116,6644.8751612791
a♭⁵,4,116,6644.8751612791
a⁵,4,117,7040.0000000000
a♯⁵,4,118,7458.6201842894
b♭⁵,4,118,7458.6201842894
b⁵,4,119,7902.1328200980
c⁶,5,120,8372.0180896192
c♯⁶,5,121,8869.8441912599
d♭⁶,5,121,8869.8441912599
d⁶,5,122,9397.2725733570
d♯⁶,5,123,9956.0634791066
e♭⁶,5,123,9956.0634791066
e⁶,5,124,10548.0818212118
f⁶,5,125,11175.3034058561
f♯⁶,5,126,11839.8215267723
g♭⁶,5,126,11839.8215267723
g⁶,5,127,12543.8539514160

UPDATE:  Changed subscripts to superscripts from c¹ through g⁶.

  • Comment on Re: utf8 hash keys revisited: How can i use musical symbols as hash keys?

Replies are listed 'Best First'.
Re^2: utf8 hash keys revisited: How can i use musical symbols as hash keys?
by karlgoethebier (Abbot) on Oct 07, 2014 at 07:21 UTC

    This is cool!

    Thank you Jim for sharing this.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      It's more extensible because, if you decide you need more enharmonic notes included in the data structure, you can easily add them. For example:

      ...
      b,-1,59,246.9416506281
      c♭¹,-1,59,246.9416506281
      b♯,0,60,261.6255653006
      c¹,0,60,261.6255653006
      d𝄫¹,0,60,261.6255653006
      c♯¹,0,61,277.1826309769
      d♭¹,0,61,277.1826309769
      c𝄪¹,0,62,293.6647679174
      d¹,0,62,293.6647679174
      e𝄫¹,0,62,293.6647679174
      ...
      

      It's also more extensible because it's a hash of a hash. More MIDI attributes can easily be added to the existing data structure as needed (e.g., absolute cents).

      (By the way, on the webpage titled MIDI Note Number and Frequency Table, there's at least one error. The frequency of MIDI note number 115, g⁵, is 6271.9269757080, not 5919.9107633862.)

        "It's more extensible..."

        Hi Jim,

        sure, this is right - no doubt.

        But don't you think that a "compound identifier" comes in more handy if you do the reverse mapping from Midi to Helmholtz like %midi_to_helmholtz = ( 70 => "a$sharp\/b$flat" );?

        Best regards, Karl

        P.S.: I skip the HoH approach for the moment, it is not the problem.

        «The Crux of the Biscuit is the Apostrophe»

        Update/clarification: Since MIDI notation does not directly support enharmonics, one way this can be emulated is via after touch. Even if the MIDI is not sent to a synth (hardware or software), the nuance can still be represented (even if imperfectly).

        To add subtlety the notes, After touch can be used on some synths. (Pitch Bend applies to all notes on a channel while After Touch applies to a specific note.)

Re^2: utf8 hash keys revisited: How can i use musical symbols as hash keys?
by RonW (Parson) on Oct 07, 2014 at 22:11 UTC
      Also, the names in your data set are actually Scientific pitch notation, not Helmholtz

      Look again. They're Helmholtz names, not scientific names. Except that I screwed up and inadvertently continued to use subscripts instead of superscripts when I got to c¹. I just fixed them.

        In this diagram, the octaves are designated by "ticks" following the note name. Your data set uses numbers, like in this other diagram.

        Maybe the diagrams are incorrect?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1103032]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found