/* * Musik.java -- provide useless multimedia functionality on PalmOS * Copyright (C) 1999-2000 Wes Biggs * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ import com.sun.kjava.*; /** * @version 0.2 (J2ME CLDC for Palm beta3) * @author Wes Biggs */ public class Musik extends Spotlet { private static Button exitButton, playButton, recordButton, stopButton, newButton, beamButton; // the Graphics object static Graphics g = Graphics.getGraphics(); private static int NUM_SOUNDS = 4; private static String[] soundNames = new String[] { "Alarm", "Click", "Low Tone", "High Tone" }; private static int[] soundValues = new int[] { Graphics.SOUND_ALARM, Graphics.SOUND_CLICK, Graphics.SOUND_CONFIRMATION, Graphics.SOUND_STARTUP }; private static RadioButton[] r = new RadioButton [NUM_SOUNDS]; private static RadioGroup rg = new RadioGroup(NUM_SOUNDS); private boolean recording; private static long lastTime; private IntVector song = new IntVector(50); private static TextField status; private static ValueSelector tempo; public static void main(String[] argv) { String helpText = "This annoying (to others) application lets you record a (not very) musical composition extemporaneously. You can then play it back in all its glory, or worse yet, beam it to your friends, if you still have any. To reset the tune currently in memory, press the 'New' button. "; new HelpDisplay(helpText,"Musik",NO_EVENT_OPTIONS).register(NO_EVENT_OPTIONS); } public Musik() { recordButton = new Button("Record", 35, 120); playButton = new Button("Play", 70, 120); stopButton = new Button("Stop", 100, 120); beamButton = new Button("Beam", 35, 145); newButton = new Button("New", 70, 145); exitButton = new Button("Exit", 100, 145); tempo = new ValueSelector("Tempo: ",1,100,10,30,77); status = new TextField("Mode: ",5,95,80,15); status.setText("Practice"); g.clearScreen(); recordButton.paint(); stopButton.paint(); beamButton.paint(); exitButton.paint(); playButton.paint(); recordButton.paint(); newButton.paint(); status.paint(); tempo.paint(); int y = 0; int i; for (i = 0; i < NUM_SOUNDS; i++) { y += 15; r[i] = new RadioButton(50,y,soundNames[i]); rg.add(r[i]); r[i].paint(); } } public void beamReceive(byte[] b) { status.setText("Receive "); song.removeAllElements(); recording = false; for (int i = 0; i < b.length; i++) { song.append(b[i] + 256 * b[++i]); } status.setText("Practice "); } public void penDown(int x, int y) { int i; if (exitButton.pressed(x,y)) { System.exit(0); } else if (tempo.pressed(x,y)) { // handles itself } else if (beamButton.pressed(x,y)) { status.setText("Beam "); // Convert IntVector into byte[] byte[] xfer = new byte [song.size() * 2]; int val; for (i = 0; i < (song.size() * 2); i++) { val = song.valueAt(i/2); xfer[i] = (byte) (val & 255); xfer[++i] = (byte) (val / 256); } beamSend(xfer); status.setText("Practice "); } else if (recordButton.pressed(x,y)) { status.setText("Record "); status.paint(); lastTime = System.currentTimeMillis(); recording = true; } else if (stopButton.pressed(x,y)) { if (recording) { song.append((int) (System.currentTimeMillis() - lastTime)); song.append(NUM_SOUNDS); recording = false; status.setText("Practice "); status.paint(); } } else if (newButton.pressed(x,y)) { recording = false; status.setText("Practice "); status.paint(); song.removeAllElements(); } else if (playButton.pressed(x,y)) { status.setText("Playback "); status.paint(); int note; //float speed = tempo.getValue() / 10; int speed = tempo.getValue() / 10; for (i = 0; i < song.size(); i++) { try { Thread.sleep((int) (song.valueAt(i) / speed)); } catch (InterruptedException e) { // do nothing } note = song.valueAt(++i); if (note < NUM_SOUNDS) g.playSound(soundValues[note]); } status.setText("Practice "); status.paint(); } else { for (i = 0; i < NUM_SOUNDS; i++) { if (r[i].pressed(x,y)) { if (recording) { song.append((int) (System.currentTimeMillis() - lastTime)); song.append(i); } r[i].handlePenDown(x,y); g.playSound(soundValues[i]); if (recording) lastTime = System.currentTimeMillis(); break; } } } } }