/* * Created by Nick on Nov 29, 2004 * */ package jmsltutorial; import com.softsynth.jmsl.*; import com.softsynth.jmsl.score.*; import com.softsynth.jmsl.score.transcribe.TranscriberListener; /** * Demonstrates use of TranscriberListener interface to compute properties of * Notes as they are being transcribed. * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * didkovn@mail.rockefeller.edu * */ public class TranscribeToot06 extends TranscribeToot05 implements TranscriberListener { /** * TranscriberListener interface . Notify that a Note has been added to * Score by the Transcriber. Examine the Note and do some things to it. */ public void noteAdded(Score score, Note note) { if (!note.isRest()) { addComputedTextToNote(note); addComputedAccentToNote(note); addComputedDynamicToNote(note); } } private int lastDynamic = -1; /** Add dynamic to Note depending on amplitude */ private void addComputedDynamicToNote(Note note) { double amplitude = note.getVelData(); // if the amplitude is low and pp has not already been notated, mark pp if (amplitude < 0.3 && lastDynamic != Note.DYNAMIC_PP) { note.setDynamic(Note.DYNAMIC_PP); lastDynamic = Note.DYNAMIC_PP; } // if the amplitude is high and ff has not already been notated, mark ff if (amplitude > 0.7 && lastDynamic != Note.DYNAMIC_FF) { note.setDynamic(Note.DYNAMIC_FF); lastDynamic = Note.DYNAMIC_FF; } } /** Add an accent if the envelope rate is fast and the amplitude is loud */ private void addComputedAccentToNote(Note note) { DimensionNameSpace dns = note.getDimensionNameSpace(); double amplitude = note.getVelData(); double rate = note.getData(dns.getDimension("rate")); if (rate > 1.0 && amplitude > 0.5) { note.setMark(Note.MARK_ACCENT); } } /** Make up lyrics. Ignore Notes which are intervals of a chord */ private void addComputedTextToNote(Note note) { if (!note.isInterval()) { String text = computeText(note); note.setText(text); setLyricTextLevel(note); } } /** * Set the Y offset of the text of the note to a constant level below the * staff. This is the same code as is used by LyricLevelTransform, which * shows up in ScoreFrame's Note menu -> "Adjust height of lyrics" */ private void setLyricTextLevel(Note note) { int offset = 30; if (note.getLevel() >= NoteFactory.LEVEL_OF_MIDDLE_C) { offset = (note.getLevel() - NoteFactory.LEVEL_OF_MIDDLE_C + 8) * Staff.SPACE_INTERVAL / 2; } note.setTextOffsetY(offset); } /** Add a randomly generated string to a note */ private String computeText(Note note) { char[] consonants = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l' }; char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; String text = ""; // if pitch higher than middle C, add consonant to beginning of text. // Why? Cuz if (note.getPitchData() > 60) text += consonants[JMSLRandom.choose(consonants.length)] + ""; text += vowels[JMSLRandom.choose(vowels.length)]; return text; } /** TranscriberListener interface */ public void notifyCarriedOverMusicShape(Score score, int currentMeasureNumber, MusicShape musicShape) { // do nothing } public void start() { super.start(); transcriber.addTranscriberListener(this); } }