Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

PGN (Chess) to MIDI

by gryphon (Abbot)
on Feb 18, 2005 at 20:13 UTC ( #432491=CUFP: print w/ replies, xml ) Need Help??

Greetings fellow monks,

I know this is probably really stupid, but a friend of mine is writing a murder mystery novel that's kinda like Twin Peaks meets geeks with shortwave radios. We were IMing about numbers stations a bit, and he wondered about whether it would be possible to take a chess game and turn the moves into a song to broadcast over shortwave.

Not being one to turn down a challenge, I said, "Possible? Anything's possible with CPAN!" So here's my attempt, rough as it is. It takes a Portable Game Notation (PGN) file, reads each game, and creates a MIDI file in the key of F. How exactly a MIDI file of a chess game broadcasted over shortwave fits into the plot, I still have no idea, and that's not my job.

The really interesting thing is that I've been listenning to some Fischer games this morning, and I'm hearing some very similar patterns toward the end-game. I'd expect a very similar openning set of sounds, but this is wicked weird.

#!/usr/bin/perl use strict; use warnings; use Chess::PGN::Parse; use MIDI::Simple; my $pgn = new Chess::PGN::Parse 'games.pgn' or die $!; while ($pgn->read_game) { $pgn->parse_game; my @song = (); new_score; set_tempo 275000; noop 'c1', 'f'; foreach (@{$pgn->moves}) { if ($_ eq 'O-O') { push @song, 8, 'qn', 'F', 8, 'hn', 'F'; } elsif ($_ eq 'O-O-O') { push @song, 8, 'qn', 'F', 8, 'qn', 'F', 8, 'hn', 'F'; } else { foreach (split('')) { if (tr/KQBN/FGAB/) { push @song, 4, 'qn', $_; } elsif (tr/Rabcdef/CDEFGAB/) { push @song, 5, 'qn', $_; } elsif (tr/gh1234/CDEFGA/) { push @song, 6, 'qn', $_; } elsif (tr/5678/BCDE/) { push @song, 7, 'qn', $_; } elsif (tr/x\+/CE/) { push @song, 8, 'qn', $_; } } $song[-2] = 'hn'; } } while (@song) { noop 'o' . shift @song; my ($l, $n) = (shift @song, shift @song); $n = 'Bf' if ($n eq 'B'); n $l, $n; } write_score join('_', $pgn->white, $pgn->black) . '.mid'; }

Question: Does anyone know how to take a WAV file and convert it into MIDI? Or take a WAV file and convert it into some sort of musical notation? I'm wondering about how difficult it would be for the listenner to record the shortwave broadcast and convert it back into PGN.

gryphon
code('Perl') || die;

Comment on PGN (Chess) to MIDI
Download Code
Re: PGN (Chess) to MIDI
by hardburn (Abbot) on Feb 18, 2005 at 20:29 UTC

    gryphon++++++

    Does anyone know how to take a WAV file and convert it into MIDI?

    Not without software similar to voice recognition. WAV contains the raw waves encoded in digital form. MIDI takes a given instrument and plays it for a given note, volume, and duration. To convert WAV -> MIDI, you need to split apart which parts of the wave form go with a given instrument (which could get pretty deep into wave theory), then choose a corresponding MIDI instrument. Very non-trvial.

    Update: Actually, if your aim is to take a simple MIDI, transfer it over shortwave into a WAV, and then decode on the other end, your task is much easier. You just need to match a single instrument to a predetermined section of the WAV. You'll have to account for noise, but that's easier than a generalized WAV -> MIDI transformation described above.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: PGN (Chess) to MIDI
by Miguel (Friar) on Feb 18, 2005 at 21:11 UTC
    WaveToMidi

    From the author:

    WaveToMidi is a program for Windows and Linux that analyzes the source audio file finding the musical notes played in this file and writing it to the destination midi file, you can analyze music you recorded with your instrument/s, your voice or from audio files you have loaded into your computer, also you can analyze audio recorded in realtime from a microphone or instrument plugged to your computer's audio input.
Re: PGN (Chess) to MIDI
by zentara (Archbishop) on Feb 18, 2005 at 22:25 UTC
    take a WAV file and convert it into MIDI

    The best one I found, and "play with" is TS-Audio-to-Midi. It is a pay-for windows program, and it works under wine. It will convert a monophonic(1 instrument) wav pretty well, and you can "whistle a tune" and assign the track to a instrument.

    Otherwise, you get ALOT of tiny sound artifacts, which you need to manually cleanup. The artifacts are generated by the complex wave interactions between the different instruments.

    But, if you can get "studio tracks" for a recording, where the different players all have a separate track, you could quite effectively convert each track separately, then combine them into a quality midi.


    I'm not really a human, but I play one on earth. flash japh
Re: PGN (Chess) to MIDI
by hsmyers (Canon) on Feb 19, 2005 at 05:50 UTC
    You know, I've translated PGN into just about everything but I've got to tell you it never occured to me to translate it into MIDI. I've done fractals into MIDI and brownian motion into MIDI, but not PGN! ++To you gryphon for a totally unexpected direction in chess software!! Would you mind if I added it to Chess::PGN::Filter? While this might be a little bit on the Acme side, it's cool enough to give a kick to users old and new.

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

      Greetings hsmyers,

      Sure, go ahead and add it to Chess::PGN::Filter. Thing is, though, I think what I've come up with is only a very minimal, thrown together idea. All I did was assign a simple one-to-one map of notation-to-note. Even with this very basic map, the musical patterns are obvious. The patterns I've been hearing in some games definately justify spending time expanding the conversion process.

      Right now, each primary piece (/KQBNR/) has a unique tone, but it's hard to tell each of them apart. And there's no distinguishment between white pieces and black pieces. I think a "better" musical scheme would be to have some way to audiably know which capital piece was being moved and by which side. I think such a scheme might require multiple notes to help audiably identify capitals. Also, and more interesting, is dynamics. I think a Qa2-f7 should be louder than a Qa2-c4, and the c4 move should be louder than a2-a4. Already a checking move and a castle are easily distinguishable, but that's about it.

      A fun experiment is taking several games between the same two players and setting the tempo very, very fast.

      gryphon
      code('Perl') || die;

        Excellant! If I add it, it gives it a friendly place to live and as you develope same, we can just continue the version thing. It might also get me to get back into things like various conversions (english to algebraic for instance) at any rate it seems like an interesting piece of reasearch. Have you thought about black using a minor key with white playing a compatible major? Tempo on forced moves could be automagically distinguished from otherwise and similar such. Lots of canvas, room for much embroidery!!

        --hsm

        "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
        Right now, each primary piece (/KQBNR/) has a unique tone, but it's hard to tell each of them apart. And there's no distinguishment between white pieces and black pieces. I think a "better" musical scheme would be to have some way to audiably know which capital piece was being moved and by which side.

        Maybe a different instrument for each piece, and a different octave for each player? 8 notes to an octave, and the board is 8x8... So, for example, an opening move of b1-c3 could be four notes (octave 1, note 2; o2,n1; o1,n3; o2,n3) of a flute, while black's first move might be d7-d5 (o3,n4; o4,n7; o3,n4; o4,n5) played on a pennywhistle. Or some such.
        x-axis, white = octave 1, notes 1-8
        y-axis, white = octave 2, notes 1-8
        x-axis, black = octave 3, notes 1-8
        y-axis, black = octave 4, notes 1-8
        pawn = pennywhistle
        rook = tuba
        knight = flute
        bishop = harpsichord
        queen = mandolin
        king = trumpet


        --
        Linux, sci-fi, and Nat Torkington, all at Penguicon 3.0
        perl -e 'print(map(chr,(0x4a,0x41,0x50,0x48,0xa)))'

      The real challenge here is to translate it into a musical form that anybody would actually listen to. There are (nowadays lots of) tools that make graphics out of sound (like CTHUGHA), and there it also depends on the parameters.

      Why not add some pseudo-random stuff or even some melodic or harmonic stuff from a database, which has to be discarded in case someone wants to decode the original information, but which would make the whole track sound better. Like in J.S. Bach's time there was a vivid scene of musical improvisation, they just noted noted some single notes or numbers which were then performed in a more elaborate way, quite similar to modern guitar notations in songbooks. But then it must be done in a way that the 'naked' melody (i.e. information) is still recognizable. I'm not a musician, and no radio specialist either. Is it really possible? If not, why not?


Re: PGN (Chess) to MIDI
by pernod (Chaplain) on Feb 19, 2005 at 11:46 UTC

    Wow! This is way cool!

    The really interesting thing is that I've been listenning to some Fischer games this morning, and I'm hearing some very similar patterns toward the end-game. I'd expect a very similar openning set of sounds, but this is wicked weird.

    I'll run this by my friend who is doing a Ph D on popular musicology ("Kraftwerk - The soundtrack of post-war Western Germany" :D)and see what he thinks. But the notion of using music to quickly find hard to discern patterns in complex data is extremely interesting IMO.

    Thank you!

    pernod
    --
    Mischief. Mayhem. Soap.

Re: PGN (Chess) to MIDI
by elwarren (Curate) on Feb 22, 2005 at 18:06 UTC
    You would lose most of the beauty of the music, but you could generate and broadcast DTMF touch-tones instead. This would be less susceptable to interference on the receiving end, and could also be decoded with different types of hardware not limited to the computer. It may (or may not) play into your friends story better...

    I think your code is way cool and would love to listen to some "classic" chess matches like Botvinnik vs Fischer. Would Kasparov vs DeepBlue sound different than a human player?

    You could really see this project take off if you targetted the recent podcasting craze. There would certainly be an audience of people that would be interested in broadcasts/podcasts of different matches.
Re: PGN (Chess) to MIDI
by crenz (Priest) on May 11, 2005 at 17:33 UTC

    Do you have a few example MIDIs and games? I'm not so much into chess, but very much into algorithmic music, so I'd appreciate some samples

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://432491]
Approved by hardburn
Front-paged by hardburn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-12-27 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls