我想做的项目就在这里。 http://ardututes.weebly.com/examples/color-piano-tcs34725
但是,处理代码中继续发生错误。
Arduino代码
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "Flora_Pianoglove.h"
#include <SoftwareSerial.h>
// we only play a note when the clear response is higher than a certain number
#define CLEARTHRESHHOLD 2000
#define LOWTONE 1000
#define HIGHTONE 2000
#define LOWKEY 64 // high C
#define HIGHKEY 76 // double high C
// our RGB -> eye-recognized gamma color
byte gammatable[256];
int prevNote = -1;
// color sensor
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS,
TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
//Check for color sensor
if (tcs.begin()) {
// Serial.println("Found sensor");
}
else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
gammatable[i] = x;
}
}
void loop() {
uint16_t clear, red, green, blue;
tcs.setInterrupt(false); // turn on LED
delay(60); // takes 50ms to read
tcs.getRawData(&red, &green, &blue, &clear);
tcs.setInterrupt(true); // turn off LED
// Serial.print("C:\t"); Serial.print(clear);
// Serial.print("\tR:\t"); Serial.print(red);
// Serial.print("\tG:\t"); Serial.print(green);
// Serial.print("\tB:\t"); Serial.print(blue);
// Figure out some basic hex code for visualization
uint32_t sum = red;
sum += green;
sum += blue;
sum = clear;
float r, g, b;
r = red;
r /= sum;
g = green;
g /= sum;
b = blue;
b /= sum;
r *= 256;
g *= 256;
b *= 256;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
// Serial.print("\t"); // what is read on processing!!!
// Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
// Serial.println();
// OK we have to find the two primary colors
// check if blue is smallest. MEME: fix for 'white'
float remove, normalize;
if ((b < g) && (b < r)) {
remove = b;
normalize = max(r-b, g-b);
}
else if ((g < b) && (g < r)) {
remove = g;
normalize = max(r-g, b-g);
}
else {
remove = r;
normalize = max(b-r, g-r);
}
// get rid of minority report
float rednorm = r - remove;
float greennorm = g - remove;
float bluenorm = b - remove;
// now normalize for the highest number
rednorm /= normalize;
greennorm /= normalize;
bluenorm /= normalize;
// Serial.println();
// strip.setPixelColor(0, strip.Color(gammatable[(int)r], gammatable[(int)g],
gammatable[(int)b];
// strip.show();
// Serial.print(rednorm); Serial.print(", ");
// Serial.print(greennorm); Serial.print(", ");
// Serial.print(bluenorm); Serial.print(" ");
// Serial.println();
float rainbowtone = 0;
if (bluenorm <= 0.1) {
// between red and green
if (rednorm >= 0.99) {
// between red and yellow
rainbowtone = 0 + 0.2 * greennorm;
}
else {
// between yellow and green
rainbowtone = 0.2 + 0.2 * (1.0 - rednorm);
}
}
else if (rednorm <= 0.1) {
// between green and blue
if (greennorm >= 0.99) {
// between green and teal
rainbowtone = 0.4 + 0.2 * bluenorm;
}
else {
// between teal and blue
rainbowtone = 0.6 + 0.2 * (1.0 - greennorm);
}
}
else {
// between blue and violet
if (bluenorm >= 0.99) {
// between blue and violet
rainbowtone = 0.8 + 0.2 * rednorm;
}
else {
// between teal and blue
rainbowtone = 0;
}
}
// Serial.print("Scalar "); Serial.println(rainbowtone);
float keynum = LOWKEY + (HIGHKEY - LOWKEY) * rainbowtone;
Serial.print(keynum); Serial.print(","); Serial.println();
// float freq = pow(2, (keynum - 49) / 12.0) * 440;
// Serial.print("Freq = "); Serial.println(freq);
Serial.print((int)r );
Serial.print(" ");
Serial.print((int)g);
Serial.print(" ");
Serial.println((int)b);
// playNote(keynum);
}
Arduino代码监控没有问题。但,
import javax.sound.midi.*;
import processing.serial.*;
Serial port;
private Synthesizer synthesizer;
private MidiChannel channel; // channel we play on -- 10 is for percussion
private Instrument[] soundbankInstruments, synthesizerInstruments;
private int velocity = 64; // default volume is 50%
private int midiNote = -1;
private int prevNote = -1;
private int program = 25;
String currentInstrument = "nothing selected yet";
PFont font;
float x,y;
int n = 100;
float [] xdata = new float[n];
void setup() {
size (600, 600);
background (127);
println(Serial.list());
//port = new Serial(this, Serial.list()[4], 9600);
//port.bufferUntil('\n'); // don't generate a serialEvent() unless you get a newline
character:
Soundbank soundbank = null;
File file = new File(dataPath("soundbank-deluxe.gm"));
try {
soundbank = MidiSystem.getSoundbank(file);
synthesizer = MidiSystem.getSynthesizer( );
synthesizer.open();
channel = synthesizer.getChannels()[1];
}
catch (Exception e) { e.printStackTrace(); }
soundbankInstruments = soundbank.getInstruments();
synthesizer.loadAllInstruments(soundbank);
synthesizerInstruments = synthesizer.getLoadedInstruments();
// for (int i = 0; i < synthesizerInstruments.length; i++) {
// println(i + " " + synthesizerInstruments[i]);
// }
channel.programChange(program);
currentInstrument = synthesizerInstruments[program].toString();
}
void draw(){
background (127);
text(program + " " + currentInstrument, 10, height - 8);
noStroke();
}
void serialEvent(Serial port) {
String inString = port.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
println(inString);
try {
String[] data = split(inString, ",");
float x = float (data[0]);
xdata[0] = x;
midiNote = int(map(x, 65, 73, 50, 80));
if (midiNote == prevNote) return;
if (midiNote == -1){
channel.noteOff(prevNote);
}
channel.noteOff(prevNote);
channel.noteOn(midiNote, 64);
prevNote = midiNote;
redraw();
}
catch (Exception e) { e.printStackTrace(); }
}
}
void keyPressed() {
if (keyCode == 37) {
program--; if (program == -1) { program = 127; }
channel.programChange(program);
currentInstrument = synthesizerInstruments[program].toString();
}
else if (keyCode == 39) {
program++; if (program == 128) { program = 0; }
channel.programChange(program);
currentInstrument = synthesizerInstruments[program].toString();
}
}
此代码的第 42 行有一个错误。错误在这里
COM4
javax.sound.midi.InvalidMidiDataException: cannot get soundbank from stream
at javax.sound.midi.MidiSystem.getSoundbank(MidiSystem.java:604)
at sketch_211215a.setup(sketch_211215a.java:53)
at processing.core.PApplet.handleDraw(PApplet.java:2432)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
请帮助我..请告诉我解决方案...