[315] | 1 | package org.apollo.meldex;
|
---|
| 2 |
|
---|
| 3 | import java.io.FileInputStream;
|
---|
| 4 | import java.io.FileNotFoundException;
|
---|
| 5 | import java.io.FileOutputStream;
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.io.ObjectInputStream;
|
---|
| 8 | import java.io.ObjectOutputStream;
|
---|
| 9 | import java.io.Serializable;
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | */
|
---|
| 14 | public class StandardisedMelody implements Serializable
|
---|
| 15 | {
|
---|
| 16 |
|
---|
| 17 | private static final long serialVersionUID = -7246438481007658500L;
|
---|
| 18 |
|
---|
| 19 | /** The array of MelodyEvents that form this melody. */
|
---|
| 20 | private MelodyEvent[] events;
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Creates a new StandardisedMelody with the given melody events.
|
---|
| 25 | *
|
---|
| 26 | * @param events The melody events that form the melody.
|
---|
| 27 | */
|
---|
| 28 | public StandardisedMelody(MelodyEvent[] events)
|
---|
| 29 | {
|
---|
| 30 | this.events = events;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Returns the length (number of melody events) of this melody.
|
---|
| 36 | *
|
---|
| 37 | * @return the length (number of melody events) of this melody.
|
---|
| 38 | */
|
---|
| 39 | public int getLength()
|
---|
| 40 | {
|
---|
| 41 | return events.length;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Returns a specified melody event from the melody.
|
---|
| 47 | *
|
---|
| 48 | * @param index The index of the required melody event.
|
---|
| 49 | *
|
---|
| 50 | * @return The melody event at the specified index.
|
---|
| 51 | */
|
---|
| 52 | public MelodyEvent getEvent(int index)
|
---|
| 53 | {
|
---|
| 54 | return events[index];
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | */
|
---|
| 60 | public void setEvent(int index, MelodyEvent event)
|
---|
| 61 | {
|
---|
| 62 | events[index] = event;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | */
|
---|
| 68 | public static void writeMelodyToFile(String melodyFileName, StandardisedMelody melody)
|
---|
| 69 | throws FileNotFoundException, IOException
|
---|
| 70 | {
|
---|
| 71 | // Open the melody file for writing serialized objects
|
---|
| 72 | FileOutputStream fos = new FileOutputStream(melodyFileName);
|
---|
| 73 | ObjectOutputStream melodyFileStream = new ObjectOutputStream(fos);
|
---|
| 74 |
|
---|
| 75 | // Write the melody to the melody file
|
---|
| 76 | melodyFileStream.writeObject(melody);
|
---|
| 77 |
|
---|
| 78 | // Flush and close the collection file
|
---|
| 79 | melodyFileStream.flush();
|
---|
| 80 | melodyFileStream.close();
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | */
|
---|
| 87 | public static StandardisedMelody readMelodyFromFile(String melodyFileName)
|
---|
| 88 | throws FileNotFoundException, IOException, ClassNotFoundException
|
---|
| 89 | {
|
---|
| 90 | // Open the melody file for reading serialized objects
|
---|
| 91 | FileInputStream fis = new FileInputStream(melodyFileName);
|
---|
| 92 | ObjectInputStream melodyFileStream = new ObjectInputStream(fis);
|
---|
| 93 |
|
---|
| 94 | // Read the melody from the melody file
|
---|
[1102] | 95 | StandardisedMelody standardisedMelody = (StandardisedMelody) melodyFileStream.readObject();
|
---|
| 96 |
|
---|
| 97 | melodyFileStream.close();
|
---|
| 98 |
|
---|
| 99 | return standardisedMelody;
|
---|
[315] | 100 |
|
---|
| 101 | }
|
---|
| 102 | }
|
---|