Ignore:
Timestamp:
05/10/18 16:04:51 (6 years ago)
Author:
davidb
Message:

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

Location:
trunk/src/org/apollo/meldex
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/apollo/meldex/PitchTracker.java

    r315 r1102  
    33import java.util.ArrayList;
    44
    5 @SuppressWarnings("unchecked") // code in java 1.4
    65public class PitchTracker
    76{
     
    1413
    1514    // The pitch data
    16     ArrayList pitchData;
     15    ArrayList<PitchValue> pitchData;
    1716
    1817    // Our lovely Pitch Period Estimators
     
    2625
    2726
    28     public ArrayList process(byte[] rawData, int rawLength, int rawSampleRate)
     27    public ArrayList<PitchValue> process(byte[] rawData, int rawLength, int rawSampleRate)
    2928    {
    3029        // Save the sample rate of the loaded sample
     
    3231
    3332        // Allocate a new pitch data array to store our pitch values
    34         pitchData = new ArrayList();
     33        pitchData = new ArrayList<PitchValue>();
    3534
    3635        // Reset our 6 pitch period estimators
  • trunk/src/org/apollo/meldex/PitchValue.java

    r315 r1102  
    1111    public PitchValue(double per, int pos)
    1212    {
    13         period = per;
    14         position = pos;
     13                period = per;
     14                position = pos;
    1515    }
    1616}
  • trunk/src/org/apollo/meldex/RogTrack.java

    r315 r1102  
    11package org.apollo.meldex;
    2 
    32
    43import java.io.File;
    54import java.io.FileInputStream;
    65import java.io.FileOutputStream;
     6import java.io.IOException;
    77import java.util.ArrayList;
    8 import javax.swing.JOptionPane;
    9 
    10 @SuppressWarnings("unchecked") // code in java 1.4
     8
     9import org.expeditee.gio.EcosystemManager;
     10
    1111public class RogTrack
    1212{
     
    2020
    2121    // Data representing the track events
    22     ArrayList trackData = new ArrayList();
     22    ArrayList<RogTrackEvent> trackData = new ArrayList<RogTrackEvent>();
    2323
    2424
     
    217217        int track_len = trackData.size();
    218218
    219         ArrayList filtered_td = new ArrayList();
     219        ArrayList<RogTrackEvent> filtered_td = new ArrayList<RogTrackEvent>();
    220220
    221221        for (int i = 0; i < track_len; i++) {
     
    235235        }
    236236
    237         ArrayList me_list = new ArrayList();
     237        ArrayList<MelodyEvent> me_list = new ArrayList<MelodyEvent>();
    238238
    239239        for (int i = 0; i < filtered_track_len-1; i++) {
     
    283283    public boolean loadFromFile(File rogFile)
    284284    {
    285         try {
    286             // Open the file with a FileInputStream, ready for parsing
    287             FileInputStream fileIn = new FileInputStream(rogFile);
    288 
    289             // Get the number of bytes in the file
    290             int rogLength = fileIn.available();
    291 
    292             // Create a buffer to store the file, and read the file into it
    293             byte[] rogData = new byte[rogLength];
    294             fileIn.read(rogData, 0, rogLength);
    295 
    296             // Parse the .rog file
    297             parseFile(rogData, rogLength);
    298         }
    299         catch (Exception ex) {
    300             JOptionPane.showMessageDialog(null, "Exception occurred reading file.\n\n" + ex);
    301             return false;
    302         }
    303 
    304         // File loaded successfully
    305         return true;
     285        boolean success = true;
     286        FileInputStream fileIn = null;
     287                try {
     288                    // Open the file with a FileInputStream, ready for parsing
     289                    fileIn = new FileInputStream(rogFile);
     290       
     291                    // Get the number of bytes in the file
     292                    int rogLength = fileIn.available();
     293       
     294                    // Create a buffer to store the file, and read the file into it
     295                    byte[] rogData = new byte[rogLength];
     296                    fileIn.read(rogData, 0, rogLength);
     297       
     298                    // Parse the .rog file
     299                    parseFile(rogData, rogLength);
     300                } catch (Exception ex) {
     301                        EcosystemManager.getGraphicsManager().showDialog("Exception", "Exception occurred reading file.\n\n" + ex);
     302                        success = false;
     303                } finally {
     304                        if (fileIn != null) {
     305                                try {
     306                                        fileIn.close();
     307                                } catch (IOException e) {
     308                                        // If an IO exception occurs when closing the file, just sweep it under the rug
     309                                }
     310                        }
     311                }
     312       
     313                // File loaded successfully
     314                return success;
    306315    }
    307316
     
    406415            // Check that the noteName is valid
    407416            if (noteName < 'A' || noteName > 'G') {
    408                 JOptionPane.showMessageDialog(null, "Parsing error - check that the Rog file is valid.");
     417                EcosystemManager.getGraphicsManager().showDialog("Exception", "Parsing error - check that the Rog file is valid.");
    409418                return;
    410419            }
     
    497506        }
    498507        catch (Exception ex) {
    499             JOptionPane.showMessageDialog(null, "Exception occurred writing to file.\n\n" + ex);
     508            EcosystemManager.getGraphicsManager().showDialog("Exception", "Exception occurred writing to file.\n\n" + ex);
    500509            return false;
    501510        }
  • trunk/src/org/apollo/meldex/StandardisedMelody.java

    r315 r1102  
    9393
    9494            // Read the melody from the melody file
    95             return (StandardisedMelody) melodyFileStream.readObject();
     95            StandardisedMelody standardisedMelody = (StandardisedMelody) melodyFileStream.readObject();
     96           
     97            melodyFileStream.close();
     98           
     99            return standardisedMelody;
    96100
    97101    }
  • trunk/src/org/apollo/meldex/Transcriber.java

    r315 r1102  
    44import java.util.ArrayList;
    55
    6 @SuppressWarnings("unchecked") // code in java 1.4
    76public class Transcriber
    87{
     
    255254    {
    256255        // Pitch track the note
    257         ArrayList pitchData = pitchTracker.process(data, length, sampleRate);
     256        ArrayList<PitchValue> pitchData = pitchTracker.process(data, length, sampleRate);
    258257
    259258        // Average the pitch data
     
    265264
    266265
    267     private int averagePitchData(ArrayList pitchData)
     266    private int averagePitchData(ArrayList<PitchValue> pitchData)
    268267    {
    269268        // Loop through the pitch values...
    270269        int i = 0, k = 0;
    271270        while (i < (pitchData.size() - 1)) {
    272             int startPos = ((PitchValue) pitchData.get(i)).position;
    273             double period = ((PitchValue) pitchData.get(i)).period;
     271            int startPos = pitchData.get(i).position;
     272            double period = pitchData.get(i).period;
    274273            double averagePeriod = period;
    275             double runningPeriod = ((PitchValue) pitchData.get(i+1)).position - startPos;
     274            double runningPeriod = pitchData.get(i+1).position - startPos;
    276275            double numPeriods = runningPeriod / period;
    277276
     
    280279            while ((i+j) < (pitchData.size() - 1)) {
    281280                // Get the next pitch estimate
    282                 period = ((PitchValue) pitchData.get(i+j)).period;
    283                 int position = ((PitchValue) pitchData.get(i+j)).position;
     281                period = pitchData.get(i+j).period;
     282                int position = pitchData.get(i+j).position;
    284283
    285284                // Make sure that this period is covered by the average so far
     
    289288
    290289                // Stop if we have covered more than 20 msec
    291                 int nextPos = ((PitchValue) pitchData.get(i+j+1)).position;
     290                int nextPos = pitchData.get(i+j+1).position;
    292291                // if ((nextPos - startPos) >= 445) {  // !! PURE-ROG !!
    293292                if ((nextPos - startPos) >= (sampleRate * 0.02)) {
     
    315314
    316315
    317     private int calculateHistogram(ArrayList data, int length)
     316    private int calculateHistogram(ArrayList<PitchValue> data, int length)
    318317    {
    319318        // This probably shouldn't be a constant
     
    328327        // Calculate the histogram data
    329328        for (int i = 0; i < (length - 1); i++) {
    330             double period = ((PitchValue) data.get(i)).period;
    331             double position = ((PitchValue) data.get(i)).position;
    332             double periodLength = ((PitchValue) data.get(i+1)).position - position;
     329            double period = data.get(i).period;
     330            double position = data.get(i).position;
     331            double periodLength = data.get(i+1).position - position;
    333332
    334333            // We only need to do this if the pitch does not equal zero
  • trunk/src/org/apollo/meldex/WavSample.java

    r315 r1102  
    88import javax.sound.sampled.AudioInputStream;
    99import javax.sound.sampled.AudioSystem;
    10 import javax.swing.JOptionPane;
    11 
    12 @SuppressWarnings("unchecked") // code in java 1.4
     10
     11import org.expeditee.gio.EcosystemManager;
     12
    1313public class WavSample
    1414{
     
    6868        }
    6969        catch (Exception ex) {
    70             JOptionPane.showMessageDialog(null, "Exception occurred creating sample.\n\n" + ex);
     70            EcosystemManager.getGraphicsManager().showDialog("Exception", "Exception occurred creating sample.\n\n" + ex);
    7171            return false;
    7272        }
     
    9090            }
    9191            catch (Exception ex) {
    92                 JOptionPane.showMessageDialog(null, "Exception occurred reloading sample.\n\n" + ex);
     92            EcosystemManager.getGraphicsManager().showDialog("Exception", "Exception occurred reloading sample.\n\n" + ex);
    9393                return false;
    9494            }
     
    101101            }
    102102            catch (Exception ex) {
    103                 JOptionPane.showMessageDialog(null, "Exception occurred resetting audio stream.\n\n" + ex);
     103            EcosystemManager.getGraphicsManager().showDialog("Exception", "Exception occurred resetting audio stream.\n\n" + ex);
    104104                return false;
    105105            }
     
    172172        if (format.getSampleSizeInBits() == 8) {
    173173            if (format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) {
    174                 JOptionPane.showMessageDialog(null, "Oops! Internal Error: Unexpected audio format (signed 8-bit).");
     174            EcosystemManager.getGraphicsManager().showDialog("Exception", "Oops! Internal Error: Unexpected audio format (signed 8-bit).");
    175175                return null;
    176176            }
     
    178178        if (format.getSampleSizeInBits() == 16) {
    179179            if (format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
    180                 JOptionPane.showMessageDialog(null, "Oops! Internal Error: Unexpected audio format (unsigned 16-bit).");
     180            EcosystemManager.getGraphicsManager().showDialog("Exception", "Oops! Internal Error: Unexpected audio format (unsigned 16-bit).");
    181181                return null;
    182182            }
     
    188188
    189189        // Create a new list to store the standardised sample values
    190         ArrayList stdTemp = new ArrayList();
     190        ArrayList<Integer> stdTemp = new ArrayList<Integer>();
    191191
    192192        // Simply pick values from the sample at the frequency specified
     
    218218
    219219
    220     private static ArrayList toStandardEndian(AudioFormat format, ArrayList inData)
    221     {
    222         // Endian details do not apply to 8-bit samples, so no conversion needed
    223         if (format.getSampleSizeInBits() == 8) {
    224             return inData;
    225         }
    226 
    227         // Create a new list to store the results
    228         ArrayList outData = new ArrayList();
    229 
    230         // Simply convert each number in the sample to the endian form for the current system
    231         for (int i = 0; i < inData.size(); i += 2) {
    232 
    233             int MSB, LSB;
    234             if (format.isBigEndian()) {
    235                 MSB = ((Integer) inData.get(i)).intValue();    // First byte is MSB (high order)
    236                 LSB = ((Integer) inData.get(i+1)).intValue();  // Second byte is LSB (low order)
    237             }
    238             else {
    239                 LSB = ((Integer) inData.get(i)).intValue();    // First byte is LSB (low order)
    240                 MSB = ((Integer) inData.get(i+1)).intValue();  // Second byte is MSB (high order)
    241             }
    242 
    243             int val = (MSB << 8) + (LSB & 255);
    244             outData.add(new Integer(val));
    245         }
    246 
    247         return outData;
    248     }
    249 
    250 
    251     private static ArrayList toUnsignedValues(AudioFormat format, ArrayList inData)
     220    private static ArrayList<Integer> toStandardEndian(AudioFormat format, ArrayList<Integer> inData)
     221    {
     222                // Endian details do not apply to 8-bit samples, so no conversion needed
     223                if (format.getSampleSizeInBits() == 8) {
     224                    return inData;
     225                }
     226       
     227                // Create a new list to store the results
     228                ArrayList<Integer> outData = new ArrayList<Integer>();
     229       
     230                // Simply convert each number in the sample to the endian form for the current system
     231                for (int i = 0; i < inData.size(); i += 2) {
     232       
     233                    int MSB, LSB;
     234                    if (format.isBigEndian()) {
     235                        MSB = ((Integer) inData.get(i)).intValue();    // First byte is MSB (high order)
     236                        LSB = ((Integer) inData.get(i+1)).intValue();  // Second byte is LSB (low order)
     237                    }
     238                    else {
     239                        LSB = ((Integer) inData.get(i)).intValue();    // First byte is LSB (low order)
     240                        MSB = ((Integer) inData.get(i+1)).intValue();  // Second byte is MSB (high order)
     241                    }
     242       
     243                    int val = (MSB << 8) + (LSB & 255);
     244                    outData.add(new Integer(val));
     245                }
     246       
     247                return outData;
     248    }
     249
     250
     251    private static ArrayList<Integer> toUnsignedValues(AudioFormat format, ArrayList<Integer> inData)
    252252    {
    253253        // PCM_UNSIGNED samples are what we want, so no conversion needed
     
    257257
    258258        // Create a new list to store the results
    259         ArrayList outData = new ArrayList();
     259        ArrayList<Integer> outData = new ArrayList<Integer>();
    260260
    261261        // Simply convert each signed number in the sample to an unsigned value
     
    275275
    276276
    277     private static ArrayList to8bit(AudioFormat format, ArrayList inData)
     277    private static ArrayList<Integer> to8bit(AudioFormat format, ArrayList<Integer> inData)
    278278    {
    279279        // 8-bit data is what we want, so no conversion necessary
     
    283283
    284284        // Create a new list to store the results
    285         ArrayList outData = new ArrayList();
     285        ArrayList<Integer> outData = new ArrayList<Integer>();
    286286
    287287        // Simply reduce each value in the sample to an 8-bit value
     
    298298
    299299
    300     private static ArrayList toMono(AudioFormat format, ArrayList inData)
     300    private static ArrayList<Integer> toMono(AudioFormat format, ArrayList<Integer> inData)
    301301    {
    302302        // Mono-channel data is what we want, so no conversion necessary
     
    306306
    307307        // Create a new list to store the results
    308         ArrayList outData = new ArrayList();
     308        ArrayList<Integer> outData = new ArrayList<Integer>();
    309309
    310310        // Simply average all of the channels for each frame
     
    362362        try {
    363363            if (AudioSystem.write(audioData, AudioFileFormat.Type.WAVE, outFile) == -1) {
    364                 JOptionPane.showMessageDialog(null, "Problem occurred writing to file.");
     364            EcosystemManager.getGraphicsManager().showDialog("Exception", "Problem occurred writing to file.");
    365365                return false;
    366366            }
    367367        }
    368368        catch (Exception ex) {
    369             JOptionPane.showMessageDialog(null, "Exception occurred saving file.\n\n" + ex);
     369            EcosystemManager.getGraphicsManager().showDialog("Exception", "Exception occurred saving file.\n\n" + ex);
    370370            return false;
    371371        }
Note: See TracChangeset for help on using the changeset viewer.