http://www.perlmonks.org?node_id=432491

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;