*
* @date 29 May 2008
*/
public static Melody provide(String fileName) throws NotValidDescriptorFileException, IOException {
// TODO Better file format description as documentation is needed.
Melody melody = new Melody();
String melodyText = "";
BufferedReader in = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = in.readLine()) != null) {
melodyText += line;
}
in.close();
StringTokenizer tokenizer = new StringTokenizer(melodyText, " ", false);
/*
* First number into the file format is timber number.
*/
if (tokenizer.hasMoreTokens() == false) {
throw (new NotValidDescriptorFileException("Timber is not available!"));
}
/*
* Timber should be positive integer number.
*/
int timber = 0;
try {
timber = Integer.parseInt(tokenizer.nextToken());
} catch (NumberFormatException ex) {
throw (new NotValidDescriptorFileException("Timber is not correct number!"));
}
/*
* Timber should be between 1 and 128.
*/
if (timber < 1 || timber > 128) {
throw (new NotValidDescriptorFileException("Timber is not between 1 and 128!"));
} else {
melody.setTimber(timber);
}
melody.setId(Melody.getUniqueId());
melody.setScore(0);
int timeCounter = 0;
/*
* All notes are fowling after timber.
*/
while (tokenizer.hasMoreTokens() == true) {
Note note = parseNote(tokenizer.nextToken());
// TODO It is not clear how much time is between two notes.
note.setOffset(timeCounter);
melody.addNote(note);
timeCounter += note.getDuration();
}
return (melody);