215 lines
7.2 KiB
Java
Executable File
215 lines
7.2 KiB
Java
Executable File
import java.io.*;
|
|
|
|
public class parsefreq
|
|
{
|
|
public parsefreq(){}
|
|
|
|
public static void main (String [] args)
|
|
{
|
|
String freqFile = "ataripitch.txt";
|
|
|
|
try
|
|
{
|
|
BufferedReader reader = new BufferedReader(new FileReader(freqFile));
|
|
|
|
/*******************************************************
|
|
* read each frequency and distortion from freqFile *
|
|
* *
|
|
* 0 Saw MIDI note 85 (Lead 5 Charang) *
|
|
* 1 Engine MIDI note 85 *
|
|
* 2 Square MIDI note 80 (Lead 1 Square) *
|
|
* 3 Bass MIDI note 81 (Lead 2 Sawtooth) *
|
|
* 4 Noise MIDI note 85 *
|
|
* 5 Lead MIDI note 81 (Lead 2 Sawtooth) *
|
|
* 6 Buzz MIDI note 85 *
|
|
* *
|
|
********************************************************/
|
|
|
|
String line;
|
|
while ((line = reader.readLine()) != null)
|
|
{
|
|
int bracket = line.indexOf("]");
|
|
int d = Integer.parseInt(line.substring(1,bracket));
|
|
int atariNoteIndex = Integer.parseInt(line.substring(bracket+2,line.indexOf("]",bracket+1)));
|
|
int midiVoice = 0; //indexed from 0
|
|
String dist = "";
|
|
switch (d)
|
|
{
|
|
case 0: midiVoice = 84; dist = "saw"; break;
|
|
case 1: midiVoice = 84; dist = "engine"; break;
|
|
case 2: midiVoice = 80; dist = "square"; break;
|
|
case 3: midiVoice = 81; dist = "bass"; break;
|
|
case 4: midiVoice = 84; dist = "noise"; break;
|
|
case 5: midiVoice = 80; dist = "lead"; break;
|
|
case 6: midiVoice = 84; dist = "buzz"; break;
|
|
}
|
|
|
|
double freq = Double.parseDouble(line.substring(line.indexOf(",")+1,line.lastIndexOf(",")));
|
|
|
|
//A440 = 69
|
|
//middle C = 60
|
|
|
|
double unroundedNoteIndex = (12*Math.log(freq/13.75)*1.442695)+9; //= 12 * log_base_2(freq/13.75) + 9
|
|
|
|
int roundedNoteIndex = (int)Math.round(unroundedNoteIndex);
|
|
String noteName = "";
|
|
switch(roundedNoteIndex%12)
|
|
{
|
|
case 0: noteName = "C"; break;
|
|
case 1: noteName = "C+"; break;
|
|
case 2: noteName = "D"; break;
|
|
case 3: noteName = "D+"; break;
|
|
case 4: noteName = "E"; break;
|
|
case 5: noteName = "F"; break;
|
|
case 6: noteName = "F+"; break;
|
|
case 7: noteName = "G"; break;
|
|
case 8: noteName = "G+"; break;
|
|
case 9: noteName = "A"; break;
|
|
case 10: noteName = "A+"; break;
|
|
case 11: noteName = "B"; break;
|
|
}
|
|
|
|
int octave = roundedNoteIndex/12-1;
|
|
noteName += Integer.toString(octave);
|
|
|
|
//total pitch bend is from 0-16383
|
|
//values below 8192 bend flat, values above 8192 bend sharp
|
|
double valuesPerCent = 40.96; //= 8192/200
|
|
//example: 75 cents sharp = 40.96(75) = 3072+8192
|
|
|
|
double errorInCents = Math.round(100.0*(unroundedNoteIndex-(double)roundedNoteIndex));
|
|
int value = 8192+(int)(errorInCents*valuesPerCent);
|
|
|
|
int bits_r = (value/8192)*64+((value%8192)/4096)*32+((value%4096)/2048)*16+((value%2048)/1024)*8+((value%1024)/512)*4+((value%512)/256)*2+((value%256)/128);
|
|
int bits_l = value%128;
|
|
//---------------------------------------//
|
|
|
|
String midiFile = "midi/"+dist+"_"+atariNoteIndex+"_"+noteName+"_"+octave+"_"+freq+"_"+(int)errorInCents+"_"+roundedNoteIndex+"_"+bits_l+"_"+bits_r+".mid";
|
|
|
|
//write to new MIDI file
|
|
FileOutputStream outputStream = new FileOutputStream(midiFile);
|
|
|
|
//----------HEADER-----------//
|
|
outputStream.write(77); //M
|
|
outputStream.write(84); //T
|
|
outputStream.write(104); //h
|
|
outputStream.write(100); //d
|
|
outputStream.write(0); //
|
|
outputStream.write(0); //
|
|
|
|
//Size of header
|
|
outputStream.write(0); //
|
|
outputStream.write(6); //Size is 6 bytes
|
|
|
|
//MIDI file format
|
|
outputStream.write(0); //
|
|
outputStream.write(1); //MIDI type 1 (multiple tracks)
|
|
|
|
//# of tracks
|
|
outputStream.write(0); //
|
|
outputStream.write(2); //2 tracks
|
|
|
|
//Time division
|
|
outputStream.write(00); //
|
|
outputStream.write(240); //240 ticks per beat
|
|
//-------END HEADER---------//
|
|
|
|
//--------TRACK DATA--------//
|
|
//Header
|
|
outputStream.write(77); //M
|
|
outputStream.write(84); //T
|
|
outputStream.write(114); //r
|
|
outputStream.write(107); //k
|
|
|
|
//# of bytes in track data
|
|
outputStream.write(0); //
|
|
outputStream.write(0); //
|
|
outputStream.write(0); //
|
|
outputStream.write(19); //19 bytes
|
|
|
|
//Tempo
|
|
outputStream.write(0); //Delta time
|
|
outputStream.write(255); //Meta event
|
|
outputStream.write(81); //Set tempo
|
|
outputStream.write(3); //Length of data
|
|
outputStream.write(7); //Microseconds per beat (next 3 bytes)
|
|
outputStream.write(161); //
|
|
outputStream.write(32); //
|
|
|
|
//Time signature
|
|
outputStream.write(0); //Delta time
|
|
outputStream.write(255); //Meta event
|
|
outputStream.write(88); //Set time signature
|
|
outputStream.write(4); //Length of data
|
|
outputStream.write(4); //Numerator
|
|
outputStream.write(2); //Denominator^2
|
|
outputStream.write(24); //Metronome
|
|
outputStream.write(8); //32nd notes per beat
|
|
|
|
//End track
|
|
outputStream.write(0); //Delta time
|
|
outputStream.write(255); //Meta event
|
|
outputStream.write(47); //End track
|
|
outputStream.write(0); //End track
|
|
//------END TRACK DATA-----//
|
|
|
|
//--------TRACK DATA--------//
|
|
//Header
|
|
outputStream.write(77); //M
|
|
outputStream.write(84); //T
|
|
outputStream.write(114); //r
|
|
outputStream.write(107); //k
|
|
|
|
//# of bytes in track data
|
|
outputStream.write(0); //
|
|
outputStream.write(0); //
|
|
outputStream.write(0); //
|
|
outputStream.write(23); //23 bytes (might be variable)
|
|
|
|
//Change voice
|
|
outputStream.write(0); //Delta time
|
|
outputStream.write(192); //Top four bits: change voice; bottom fout bits: channel 0
|
|
outputStream.write(midiVoice); //MIDI voice
|
|
|
|
//Change volume
|
|
outputStream.write(0); //Delta time
|
|
outputStream.write(176); //Top four bits: controller event; bottom four bits: channel 0
|
|
outputStream.write(7); //Change volume
|
|
outputStream.write(80); //Volume is set to 80
|
|
|
|
//Note on
|
|
outputStream.write(0); //Delta time
|
|
outputStream.write(144); //Top four bits: note on; bottom four bits: channel 0
|
|
outputStream.write(roundedNoteIndex); //MIDI note value (0-127)
|
|
outputStream.write(100); //Velocity
|
|
|
|
//Pitch bend
|
|
outputStream.write(0); //Delta time
|
|
outputStream.write(224); //Top four bits: pitch bend; bottom four bits: channel 0
|
|
outputStream.write(bits_l); //pitch bend value
|
|
outputStream.write(bits_r); //pitch bend value
|
|
|
|
//Tick delay
|
|
outputStream.write(120); //delay- eighth note (120 ticks)
|
|
|
|
//Note off
|
|
outputStream.write(128); //Top four bits: note off; bottom four bits: channel 0
|
|
outputStream.write(roundedNoteIndex); //MIDI note value (0-127)
|
|
outputStream.write(100); //Velocity
|
|
|
|
//End track
|
|
outputStream.write(0); //Delta time
|
|
outputStream.write(255); //Meta event
|
|
outputStream.write(47); //End track
|
|
outputStream.write(0); //End track
|
|
//------END TRACK DATA-----//
|
|
|
|
outputStream.close();
|
|
}
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
System.out.println(e.getMessage());
|
|
}
|
|
}
|
|
} |