MIDI::Ngram - Find the top repeated note phrases of MIDI files
version 0.1808
use MIDI::Ngram; # Analyze a tune and build a new MIDI file of repetitions my $mng = MIDI::Ngram->new( in_file => [ 'eg/twinkle_twinkle.mid' ], ngram_size => 3, min_phrases => 3, max_phrases => 0, # Analyze all events patches => [qw( 68 69 70 71 72 73 )], random_patch => 1, ); $mng->process; # Analyze print Dumper $mng->dura_notes; print Dumper $mng->duras; print Dumper $mng->notes; print Dumper $mng->dura_note_net; print Dumper $mng->dura_net; print Dumper $mng->note_net; my $playback = $mng->populate; print $playback; $mng->write; # Convert a MIDI number string to a duration or note name. my $named = $mng->dura_convert('96'); # qn $named = $mng->note_convert('60 61,62'); # C4 Cs4,D4
MIDI::Ngram parses a given list of MIDI files, finds the top repeated note phrases, builds the transition networks, and renders to a MIDI file if desired.
MIDI::Ngram
Note: This module currently assumes that all given in_files have the same MIDI ppqn (given by the first $opus->ticks seen).
$opus->ticks
Required. An Array reference of MIDI files to process.
Ngram phrase size.
Default: 2
2
Integer. Allow a minimum of this number of ngram occurrences in both the repetition and network lists.
The maximum number of phrases to analyze/play.
Default: 0
0
Setting this to a value of 0 analyzes all phrases.
Beats per minute.
Default: 100
100
The optional MIDI note durations to choose from (at random).
Default: [] (i.e. use the computed dura phrases instead)
[]
Using a setting of ['qn'] allows you to evenly inspect the phrases during audio playback.
['qn']
The patches to choose from (at random) if given the random_patch option.
Default: [0 .. 127]
[0 .. 127]
Boolean. Choose a random patch from patches for each channel.
Default: 0 (meaning "use the piano patch")
MIDI output file.
Default: midi-ngram.mid
midi-ngram.mid
Insert a rest of the given duration after each phrase.
Default: '' (no resting)
''
Array reference of the channels to analyze. If not given, all channels are analyzed.
Default: undef
undef
The number of times to choose a weighted phrase. * This only works in conjunction with the weight option.
Default: 10
10
Boolean. Play phrases according to the probability of their repetition occurrence with the function "choose_weighted" in List::Util::WeightedChoice.
Boolean. Shuffle the non-weighted phrases before playing them.
Boolean. Accumulate phrases onto a single playback channel.
The score object in "MAIN-ROUTINES" in MIDI::Simple. Constructed by the populate method.
The hash-reference bucket of duration*note ngrams. Constructed by the process method.
duration*note
The combination of duration with a note is indicated with the * character. For example: ten*Cs4
*
ten*Cs4
The hash-reference bucket of pitch ngrams. Constructed by the process method.
Notes are represented in MIDI format with octave number like, Cs4. A cluster of repeated notes is separated by spaces. Simultaneous notes are separated with a comma (,). For example: A4,Cs5.
Cs4
,
A4,Cs5
The hash-reference bucket of duration ngrams. Constructed by the process method.
Durations are represented in MIDI::Simple style format. For example, hn for a known length and d123 for a tick duration. A cluster of repeated durations is separated by spaces. Simultaneous durations are separated with a comma (,). For example: hn,d85.
hn
d123
hn,d85
A hash-reference ngram transition network of the durations.
A dash or hyphen (-) divides nodes. For example: qn,hn qn-qn,hn qn where the nodes are both qn,hn qn and qn,hn qn.
-
qn,hn qn-qn,hn qn
qn,hn qn
A hash-reference ngram transition network of the notes.
A dash or hyphen (-) divides nodes. For example: A4 G4,E3-F4,D3 F4 where the nodes are A4 G4,E3 and F4,D3 F4.
A4 G4,E3-F4,D3 F4
A4 G4,E3
F4,D3 F4
A hash-reference ngram transition network of the durations and notes considered as a unit.
A dash or hyphen (-) divides nodes. For example: qn*C3 qn*G4,hn*E3-qn*G4 qn*F4,hn*F3 where the nodes are qn*C3 qn*G4,hn*E3 and qn*G4 qn*F4,hn*F3.
qn*C3 qn*G4,hn*E3-qn*G4 qn*F4,hn*F3
qn*C3 qn*G4,hn*E3
qn*G4 qn*F4,hn*F3
$mng = MIDI::Ngram->new(%arguments);
Create a new MIDI::Ngram object.
$mng->process;
Find all ngram phrases of durations, notes and both combined.
This method creates the dura_notes, dura, and notes repetition lists.
$playback = $mng->populate;
Add notes to the MIDI score and return the playback notes.
$mng->write;
Write out the MIDI file.
$durations = $mng->dura_convert($string);
Convert MIDI numbers to named durations.
$notes = $mng->note_convert($string);
Convert MIDI numbers to named notes.
$dura_notes = $mng->dura_note_convert($string);
Convert MIDI numbers to named duration*note strings.
Moo
Lingua::EN::Ngram
List::Util
List::Util::WeightedChoice
Music::Note
MIDI::Util
Gene Boggs <gene@cpan.org>
This software is copyright (c) 2022 by Gene Boggs.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install MIDI::Ngram, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MIDI::Ngram
CPAN shell
perl -MCPAN -e shell install MIDI::Ngram
For more information on module installation, please visit the detailed CPAN module installation guide.