Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by karlgoethebier (Abbot)
on Oct 06, 2014 at 19:54 UTC ( [id://1103004]=perlquestion: print w/replies, xml ) Need Help??

karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

for some musical analysis i want to map musical notes notated in Helmholtz pitch notation to MIDI note numbers.

I tried this:

#!/usr/bin/env perl + use warnings; use strict; use charnames qw(:full); use Data::Dump; # use utf8; binmode STDOUT, ":utf8"; my $flat = chr( charnames::vianame(qq(MUSIC FLAT SIGN)) ); my $sharp = chr( charnames::vianame(qq(MUSIC SHARP SIGN)) ); print qq( b$flat\n); print qq( a$sharp\n); my %helmholtz_to_midi = ( "a$sharp\/b$flat" => 70 ); dd \%helmholtz_to_midi; __END__

I get:

b♭
a♯
{ "a\x{266F}/b\x{266D}" => 70 }

But i want something like this:

{ "a♯/b♭" => 70 }

How can i do that?

Short and very simplified explanation why i need such strange keys:

Every musical note in the western system of music has at least two incarnations (there are many more, but i skip the details for the moment).

In this case this is b♭ or a♯. For example: if it is a♯, it may belong to the tonality of F♯ where the note is the major third.

But if written as b♭, the tonality may be F where it is a perfect fourth. It depends on the context.

Thank you very much for any hint and best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

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

Replies are listed 'Best First'.
Re: utf8 hash keys revisited: How can i use musical symbols as hash keys?
by kennethk (Abbot) on Oct 06, 2014 at 20:06 UTC
    "a\x{266F}/b\x{266D}" is equivalent to "a♯/b♭". You should see your expected result if you print join "\n", keys %helmholtz_to_midi. Data::Dump outputs in an escaped form, just in case.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thank you kennethk for advice. I jumped to the wrong conclusion ;-) It works as expected.

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: utf8 hash keys revisited: How can i use musical symbols as hash keys?
by Your Mother (Archbishop) on Oct 06, 2014 at 20:13 UTC

    kennethk already answered, just chiming in with the name notation version because it makes inlining stuff more legible (than some approaches).

    perl -CSD -Mcharnames=:full -le 'print "\N{MUSICAL SYMBOL GLISSANDO UP}a\N{MUSIC SHARP SIGN}"' 𝆱a♯

      Yes Your Mother, $sharp = qq(\N{MUSIC SHARP SIGN}); looks much more concise.

      Thank you very much for the hint and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      Don't even need use charnames ':full'; any more.
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

    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⁶.

      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.)

        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.

Re: utf8 hash keys revisited: How can i use musical symbols as hash keys?
by Laurent_R (Canon) on Oct 06, 2014 at 21:14 UTC
    This is a bit irrelevant, but while A sharp and B flat are the same key on a piano and many other instruments (and indeed are the same in an equal-tempered tuning system), I am really not convinced that a virtuoso violonist or cellist, or an opera singer, would consider them as equivalent and would play exactly the same tone.
      "This is a bit irrelevant..."

      Although this a bit OT for this forum i must tell you that it is relevant.

      Enharmonics are important for the harmonical context as i wrote already in the readmore section of my OP.

      "... a virtuoso violonist or cellist, or an opera singer, would consider them as equivalent and would play exactly the same tone."

      Some do and some not ;-) You can count yourself lucky if you can listen to a string quartett that does this right.

      A good explanation of the problem is Intonation und reine Stimmung, unfortunatly in german.

      See especially the section about the implications of tuning the violin in pure perfect fifths. For this see also Syntonic comma.

      This article might be also of interest, but be careful: "SuperCollider programming considered harmful".

      Btw, many years ago i attended a lecture of Johannes Fritsch where he demonstrated some of this phenomenons on a self-made Polychord. Very instructive and impressive. He could even whistle intervals in different temperaments.

      Edit: Fixed wrong term. Perfect isn't pure ;-)

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        Hi Karl,

        thanks for your answers. I am a very very very poor musician, having played clarinet for only about 15 years (I stopped when I started to program, unfortunately, although I am not entirely sure that it is related). BTW, I started to learn clarinet when I was living in your country (1980-1988) I wasn't doing too bad with clarinet after those 15 years, but it is clear that I am not, have never been and will never be a good amateur musician. I have a poor ear. And if I had no money and was going to be going to try to sing in the Paris metro to try to get a bit of money, the best I would get is money from people asking me to stop driving them mad. ;-)

        Having said all that, I have been very interested with the theory of music and acoustics, which is why I picked up on that. My own ear is certainly unable to hear the different between an A sharp and a B flat on a violin or in a string quartet.

        Yet, I have had the chance in the past (late 1980s) to meet a number of times with the members of the Amadeus Quartet, in my view most probably the best string quartet (or perhaps even the best chamber music ensemble) of the last quarter of the 20th century, and discuss these matters and many others with them, especially by their first violin, Norbert Brainin. It was very interesting, as you can imagine, to meet such dedicated and universally recognized professionals. I had to a large extent a math view of the subject (equal temperament, easy things, logarithms), although I knew that even a perfect fifth does not match the mathematical notion, but they (especially Norbert) changed my view of the subject, even though my musical knowledge and capabilities were close to zero compared to them.

        It is this experience that prompted me to answer your post, although it was perhaps not irrelevant, but certainly off-topic. I still thought it was interesting to raise the subject.

        Cheers, Laurent.

      For details, see Comma (music).
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: utf8 hash keys revisited: How can i use musical symbols as hash keys?
by Anonymous Monk on Oct 07, 2014 at 14:04 UTC
    Many computer-nerds are also music-nerds. :-)

      Musicians are nerds since hundreds of years. I think they are the real nerds. Try to get invited to a After Show-Party of a classical concert, or even better: enroll to some master class as a passive participant (it's not so expensive) regardless what instrument. And you will see the truth.

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: utf8 hash keys revisited: How can i use musical symbols as hash keys?
by sundialsvc4 (Abbot) on Oct 08, 2014 at 00:22 UTC
    J. S. Bach was one of the great music-nerds, but there were and still are a lot of others.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1103004]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-19 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found