I have the following code:
import processing.video.*;
import oscP5.*;
import netP5.*;
//sending the data to wekinator
int numPixelsOrig, numPixels, boxWidth = 64, boxHeight = 48, numHoriz = 640/boxWidth, numVert = 480/boxHeight;
color[] downPix = new color[numHoriz * numVert];
PGraphics buffer;
Capture video;
OscP5 oscSend;
//recieving data from the wekinatorino
ArrayList<Blob> blobs = new ArrayList<Blob>();
int amt = 5;
int cons1 = 200, cons2 = 150;
float xspeed, yspeed, radius;
float p1, p2, p3, p4, p5, p6;
OscP5 oscRecieve;
NetAddress dest;
void setup() {
colorMode(RGB, 100);
size(600, 600, P2D);
buffer = createGraphics(600, 600, P3D);
buffer.beginDraw();
buffer.colorMode(HSB, 100);
buffer.endDraw();
String[] cameras = Capture.list();
if (cameras == null) {
video = new Capture(this, 640, 480);
}
if (cameras.length == 0) {
exit();
} else {
video = new Capture(this, 640, 480);
video.start();
numPixelsOrig = video.width * video.height;
}
oscSend = new OscP5(this, 9000);
oscRecieve = new OscP5(this, 12000);
dest = new NetAddress("127.0.0.1", 6448);
}
void draw() {
//println(blobs.size());
if (video.available() == true) {
video.read();
video.loadPixels();
int boxNum = 0;
int tot = boxWidth*boxHeight;
for (int x = 0; x < 640; x += boxWidth) {
for (int y = 0; y < 480; y += boxHeight) {
float red = 0, green = 0, blue = 0;
for (int i = 0; i < boxWidth; i++) {
for (int j = 0; j < boxHeight; j++) {
int index = (x + i) + (y + j) * 640;
red += red(video.pixels[index]);
green += green(video.pixels[index]);
blue += blue(video.pixels[index]);
}
}
downPix[boxNum] = color(red/tot, green/tot, blue/tot);
fill(downPix[boxNum]);
int index = x + 640*y;
red += red(video.pixels[index]);
green += green(video.pixels[index]);
blue += blue(video.pixels[index]);
noStroke();
rect(x, y, boxWidth, boxHeight);
boxNum++;
}
}
if (frameCount % 2 == 0)
sendOsc(downPix);
}
if (blobs.size() < amt) {
blobs.add(new Blob(new PVector(random(100 + (width - 200)), random(100 + (height- 200))), new PVector(-3, 3), random(100, 300)));
}
for (int i = blobs.size() - 1; i >= 0; i--) {
if (blobs.size() > amt) {
blobs.remove(i);
}
}
buffer.beginDraw();
buffer.loadPixels();
for (int x = 0; x < buffer.width; x++) {
for (int y = 0; y < buffer.height; y++) {
int index = x + y * buffer.width;
float sum = 0;
for (Blob b : blobs) {
float d = dist(x, y, b.pos.x, b.pos.y);
sum += 10 * b.r / d;
}
buffer.pixels[index] = color(sum, 255, 255); //constrain(sum, cons1, cons2)
}
}
buffer.updatePixels();
buffer.endDraw();
for (Blob b : blobs) {
b.update();
}
//if () {
//xspeed = map(p1, 0, 1, -5, 5);
//yspeed = map(p2, 0, 1, -5, 5);
//radius = map(p3, 0, 1, 100, 300);
//cons1 = int(map(p4, 0, 1, 0, 255));
//cons2 = int(map(p5, 0, 1, 0, 255));
//amt = int(map(p6, 0, 1, 1, 6));
//for (Blob b : blobs) {
// b.updateAlgorithm(xspeed, yspeed, radius);
//}
//}
image(buffer, 0, 0);
}
void sendOsc(int[] px) {
//println(px);
OscMessage msg = new OscMessage("/wek/inputs");
for (int i = 0; i < px.length; i++) {
msg.add(float(px[i]));
}
oscSend.send(msg, dest);
}
void oscEvent(OscMessage theOscMessage) {
if (theOscMessage.checkAddrPattern("/wek/outputs")==true) {
if (theOscMessage.checkTypetag("fff")) {
p1 = theOscMessage.get(0).floatValue();
p2 = theOscMessage.get(1).floatValue();
p3 = theOscMessage.get(2).floatValue();
p4 = theOscMessage.get(2).floatValue();
p5 = theOscMessage.get(2).floatValue();
p6 = theOscMessage.get(2).floatValue();
} else {
}
}
}
void mousePressed() {
xspeed = random(-5, 5);
yspeed = random(-5, 5);
radius = random(100, 300);
cons1 = int(random(255));
cons2 = int(random(255));
amt = int(random(6));
for (Blob b : blobs) {
b.updateAlgorithm(xspeed, yspeed, radius);
}
}
class Blob {
PVector pos;
PVector vel;
float r;
Blob(PVector pos, PVector vel, float r) {
this.pos = pos.copy();
this.vel = vel.copy();
this.r = r;
}
void update(){
pos.add(vel);
if (pos.x > width || pos.x < 0) {
vel.x *= -1;
}
if (pos.y > height || pos.y < 0) {
vel.y *= -1;
}
}
void updateAlgorithm(float vx, float vy, float nr){
vel.x = vx;
vel.y = vy;
r = nr;
}
}
Then I create some graphics in the buffer element. but the graphics aren't using my HSB color mode with the result i only see blue and white...
so how do i correct my code, or change the colorMode for a PGraphics element to HSB?
According to the PGraphics reference:
The beginDraw() and endDraw() methods (see above example) are
necessary to set up the buffer and to finalize it
therefore you should try this:
buffer = createGraphics(600, 600, P3D);
buffer.beginDraw();
buffer.colorMode(HSB, 100);
buffer.endDraw();
Here's a full test sketch to run and compare:
PGraphics buffer;
void setup(){
colorMode(RGB, 100);
size(600, 600, P2D);
//draw test gradient in RGB buffer
noStroke();
for(int i = 0 ; i < 10; i++){
fill(i * 10,100,100);
rect(0,i * 60,width,60);
}
buffer = createGraphics(600, 600, P3D);
buffer.beginDraw();
buffer.colorMode(HSB, 100);
buffer.endDraw();
//draw test gradient in HSB buffer
buffer.beginDraw();
buffer.noStroke();
for(int i = 0 ; i < 10; i++){
buffer.fill(i * 10,100,100);
buffer.rect(0,i * 60,width,60);
}
buffer.endDraw();
//finally render the buffer on screen, offset to the right for comparison
image(buffer,300,0);
}
Related
so I'm creating an interactive story, and I want to switch between scenes by clicking buttons (like a standard old pokemon game except there isn't movement, just clicking).
I should probably use a switch statement right? But I don't know how to implement the changing of scenes and loading scenes into the cases. I also don't know if I would need to use 'if' statements within the cases.
This might be confusing, because it is very confusing to me. I'd appreciate any help you guys offer! I am desperate ;)
Code so far is below:
//PImages
PImage startScreen;
PImage[] waves = new PImage[3];
PImage[] scenes = new PImage[1];
int switchVariable;
//Objects
Button play;
void setup() {
size(750, 600);
background(#A3E9EA);
//Initialising Objects
play = new Button(50, 480, 330);
//loading wave images
waves[0] = loadImage("wave1.png");
waves[1] = loadImage("wave2.png");
waves[2] = loadImage("wave3.png");
//loading start image
startScreen = loadImage("start-screen.png");
//loading scenes
scenes[0] = loadImage("scene-one.png");
//setting frame rate
frameRate(6);
}
void draw() {
background(#A3E9EA);
frameCount++;
println (frameCount);
//drawing wave animation
if (frameCount < 5) {
image(waves[0], 0, 0);
}
else {
image(waves[1], 0, 0);
}
if (frameCount < 15 & frameCount > 10) {
background(#A3E9EA);
image(waves[2], 0, 0);
frameCount = 0;
}
//drawing start screen
image(startScreen, 0, 0);
//displaying play button
if (play.visible) play.buttonDisplay();
}
void mousePressed() {
if (play.visible) {
float d = dist(play.x+110, play.y+22, mouseX, mouseY);
if (d <= play.radius){
background(#A3E9EA);
image(scenes[0], 0, 0);
}
}
}
Button Class:
class Button {
float radius;
float x;
float y;
PImage[] buttonImage = new PImage[2];
boolean visible;
Button(float _radius, float _x, float _y) {
radius = _radius;
visible = true;
x = _x;
y = _y;
buttonImage[0] = loadImage("play-game-button.png");
}
void buttonDisplay() {
image(buttonImage[0], x, y);
}
}
The source code below shows one possible approach to your question. It does not rely on ‘switch’ but instead uses a custom control with up and down buttons similar to a Java stepper. The stepper control value corresponds with images in the scenes array and the background() is set accordingly. A separate button class is required and code for this follows the demo.
color BLUE = color(64, 124, 188);
color LTGRAY = color(185, 180, 180);
color YELLOW = color(245, 250, 13);
color RED = color(255, 0, 0);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);
color GREEN = color(0, 255, 0);
color ORANGE = color(247, 168, 7);
PFont font;
PImage[] scenes = new PImage[6];
// **** Up/Down Buttons init, min, max values **** //
final int _initValue = 2;
final int _maxValue = 5;
final int _minValue = 0;
int stepperValue = 0;
Button _up;
Button _dwn;
Button _quit;
final int _displayX = 160;
final int _displayY = 30;
final int _displayW = 100;
final int _displayH = 24;
final int _txtSize = 18;
void stepperValueDisplay(int value) {
fill(WHITE); // background color
noStroke();
rect(_displayX, _displayY, _displayW, _displayH, 0);
fill(BLACK); // text color
textSize(_txtSize);
textAlign(CENTER);
String str = String.format("Scene %d", value);
text(str, _displayX, _displayY, _displayW, _displayH);
}
void scene_0(){
background(RED);
save("scene0.png");
}
void scene_1(){
background(GREEN);
save("scene1.png");
}
void scene_2(){
background(BLACK);
save("scene2.png");
}
void scene_3(){
background(YELLOW);
save("scene3.png");
}
void scene_4(){
background(LTGRAY);
save("scene4.png");
}
void scene_5(){
background(ORANGE);
save("scene5.png");
}
void setup() {
size(600, 600);
background(BLUE);
font = createFont("Menlo-Bold", 20);
_dwn = new Button( _displayX - 40, _displayY, 40, 24, "--", LTGRAY, BLACK);
_up = new Button( _displayX + _displayW, _displayY, 40, 24, "++", LTGRAY, BLACK);
stepperValue = _initValue;
_quit = new Button(width - 60, 20, 30, 24, "Q", LTGRAY, BLACK);
scene_0();
scene_1();
scene_2();
scene_3();
scene_4();
scene_5();
scenes[0] = loadImage("scene0.png");
scenes[1] = loadImage("scene1.png");
scenes[2] = loadImage("scene2.png");
scenes[3] = loadImage("scene3.png");
scenes[4] = loadImage("scene4.png");
scenes[5] = loadImage("scene5.png");
}
void draw() {
background(scenes[stepperValue]);
_up.display();
_dwn.display();
_quit.display();
stepperValueDisplay(stepperValue);
}
void mousePressed() {
if (mouseX > _quit.x && mouseX < _quit.x + _quit.w && mouseY > _quit.y && mouseY < _quit.y + _quit.h) {
exit();
}
if (mouseX > _up.x && mouseX < _up.x + _up.w && mouseY > _up.y && mouseY < _up.y + _up.h) {
stepperValue++;
if (stepperValue > _maxValue) {
stepperValue = _maxValue;
}
stepperValueDisplay(stepperValue);
}
if (mouseX > _dwn.x && mouseX < _dwn.x + _dwn.w && mouseY > _dwn.y && mouseY < _dwn.y + _dwn.h) {
stepperValue--;
if (stepperValue < _minValue) {
stepperValue = _minValue;
}
stepperValueDisplay(stepperValue);
}
}
Button Class:
int _btnTxtSize = 18;
class Button {
float x, y, w, h;
String title;
color bkgrndColor;
color txtColor;
// Constructor
Button(int xpos, int ypos, float wt, float ht, String titleStr, color background, color textColor) {
x = xpos;
y = ypos;
w = wt;
h = ht;
title = titleStr;
bkgrndColor = background;
txtColor = textColor;
}
void display(){
fill(bkgrndColor);
noStroke();
rect( x, y, w, h, 0);
fill(txtColor);
textSize(_btnTxtSize);
textAlign(CENTER);
text(title, x, y, w, h);
}
}
I have a PyTorch model and try run it on iOS. I have the next code:
at::Tensor tensor2 = torch::from_blob(imageBuffer2, {1, 1, 256, 256}, at::kFloat);
c10::InferenceMode guard;
auto output = _impl.forward({tensor1, tensor2});
torch::Tensor tensor_img = output.toTuple()->elements()[0].toTensor();
My question is "How I can convert tensor_img to UIImage?"
I found that functions in PyTorch documentation:
- (UIImage*)convertRGBBufferToUIImage:(unsigned char*)buffer
withWidth:(int)width
withHeight:(int)height {
char* rgba = (char*)malloc(width * height * 4);
for (int i = 0; i < width * height; ++i) {
rgba[4 * i] = buffer[3 * i];
rgba[4 * i + 1] = buffer[3 * i + 1];
rgba[4 * i + 2] = buffer[3 * i + 2];
rgba[4 * i + 3] = 255;
}
size_t bufferLength = width * height * 4;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rgba, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
if (colorSpaceRef == NULL) {
NSLog(#"Error allocating color space");
CGDataProviderRelease(provider);
return nil;
}
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpaceRef,
bitmapInfo,
provider,
NULL,
YES,
renderingIntent);
uint32_t* pixels = (uint32_t*)malloc(bufferLength);
if (pixels == NULL) {
NSLog(#"Error: Memory not allocated for bitmap");
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
return nil;
}
CGContextRef context = CGBitmapContextCreate(pixels,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpaceRef,
bitmapInfo);
if (context == NULL) {
NSLog(#"Error context not created");
free(pixels);
}
UIImage* image = nil;
if (context) {
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
if ([UIImage respondsToSelector:#selector(imageWithCGImage:scale:orientation:)]) {
float scale = [[UIScreen mainScreen] scale];
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
} else {
image = [UIImage imageWithCGImage:imageRef];
}
CGImageRelease(imageRef);
CGContextRelease(context);
}
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
CGDataProviderRelease(provider);
if (pixels) {
free(pixels);
}
return image;
}
#end
If I correctly understand, that function can convert unsigned char * to UIImage. I think that I need convert my tensor_img to unsigned char*, but I don't understand how I can do it.
The 1st code its a torch bridge and 2nd code is UIImage helper which I run from Swift. Anyway, I resolve that issue, we can close it. Code example:
for (int i = 0; i < 3 * width * height; i++) {
[results addObject:#(floatBuffer[i])];
}
NSMutableData* data = [NSMutableData dataWithLength:sizeof(float) * 3 * width * height];
float* buffer = (float*)[data mutableBytes];
for (int j = 0; j < 3 * width * height; j++) {
buffer[j] = [results[j] floatValue];
}
return buffer;
I have a problem where I kwow that my bluetooth module (HC-05) is connected to a processing sketch, but nothing ever gets sent from the processing sketch,why?
No error messages but the info never gets sent over because I never see it in Arduino, where it would be printed to the Serial Monitor.
Full Processing Code:
import processing.serial.*;
import controlP5.*;
Serial myPort;
ControlP5 cp5;
int slider1 = 0;
int slider2 = 0;
int slider3 = 0;
void setup() {
size(800, 800);
cp5 = new ControlP5(this);
PFont roboto = createFont("Roboto-Bold.ttf", 1, true);
ControlFont font = new ControlFont(roboto, 28);
Controller Aslider1 = cp5.addSlider("slider1")
.setPosition(85, 100)
.setCaptionLabel("Red")
.setRange(0, 255)
.setWidth(191)
.setHeight(50);
cp5.getController("slider1").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
cp5.getController("slider1").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
Controller Aslider2 = cp5.addSlider("slider2")
.setPosition(301, 100)
.setCaptionLabel("Green")
.setRange(0, 255)
.setWidth(191)
.setHeight(50);
cp5.getController("slider2").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
cp5.getController("slider2").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
Controller Aslider3 = cp5.addSlider("slider3")
.setPosition(517, 100)
.setCaptionLabel("Blue")
.setRange(0, 255)
.setWidth(191)
.setHeight(50);
cp5.getController("slider3").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
cp5.getController("slider3").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
myPort = new Serial(this, "COM5", 9600);
}
void draw() {
background(255, 100, 100);
fill(255);
stroke(1);
rectMode(CENTER);
rect(width/2, 350, 200, 75);
fill(0);
textSize(32);
text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
if (mousePressed) {
fill(100);
stroke(1);
rectMode(CENTER);
rect(width/2, 350, 200, 75);
fill(0);
textSize(32);
text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
} else {
fill(170);
stroke(1);
rectMode(CENTER);
rect(width/2, 350, 200, 75);
fill(0);
textSize(32);
text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
}
}
}
void mouseReleased() {
if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
fill(100);
stroke(1);
rectMode(CENTER);
rect(width/2, 350, 200, 75);
fill(0);
textSize(32);
text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
println(hex(color(int(slider1), int(slider2), int(slider3))).toString().substring(2));
myPort.write(hex(color(int(slider1), int(slider2), int(slider3))).toString().substring(2));
myPort.clear();
}
}
Full Arduino Code:
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
const int PIN = 6;
const int NUMPIXELS = 30;
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial mySerial (rxPin, txPin);
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
unsigned long currentMillis;
unsigned long loopMillis;
unsigned long waitMillis;
int interval = 100;
int waitInterval = 0;
int redBrightness = 0;
int greenBrightness = 0;
int blueBrightness = 0;
bool wait = false;
bool goToRed = true;
bool goToOrange = false;
bool goToYellow = false;
bool goToGreen = false;
bool goToAqua = false;
bool goToPurple = false;
String hexValue;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mySerial.begin(9600);
pixels.begin();
pixels.show();
}
void loop() {
if (mySerial.available() > 0) {
hexValue = mySerial.read();
Serial.println(hexValue);
if (hexValue != "shiftN" || hexValue != "shiftY") {
shiftMode = false;
// value is a hex
redBrightness = hexValue.substring(0, 2).toInt();
greenBrightness = hexValue.substring(2, 4).toInt();
greenBrightness = hexValue.substring(4, 6).toInt();
} else {
if (hexValue = "shiftY") {
shiftMode = true;
} else {
shiftMode = false;
}
}
}
pixels.show();
setColor(redBrightness, greenBrightness, blueBrightness);
}
void setColor(int red, int green, int blue) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, red, green, blue);
}
}
Thanks for the help, cheers!
Other links:
https://discourse.processing.org/t/nothing-gets-sent-to-arduino-from-processing/13654
https://arduino.stackexchange.com/questions/68233/nothing-gets-sent-to-arduino-from-processing
https://forum.arduino.cc/index.php?board=11.0
Assuming myPort is an output stream, it may be buffered. If so, try flushing it after writing:
myPort.flush();
check if hc-05 9600 baud or 38400 baud
For everyone wondering, here is updated code:
Arduino:
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
const int pin = 6;
const int numOfPixels = 30;
int r = 0;
int g = 0;
int b = 0;
Adafruit_NeoPixel pixels(numOfPixels, pin, NEO_GRB + NEO_KHZ800);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pixels.begin();
pixels.show();
pinMode(11, OUTPUT);
}
void loop() {
pixels.show();
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
Serial.println(String(Serial.read()));
r = Serial.parseInt();
g = Serial.parseInt();
b = Serial.parseInt();
setColor(r, g, b);
}
serialFlush();
}
void setColor(int red, int green, int blue) {
pixels.fill(pixels.Color(red, green, blue), 0, numOfPixels);
Serial.println("Red");
Serial.println(red);
Serial.println("Green");
Serial.println(green);
Serial.println("Blue");
Serial.println(blue);
}
void serialFlush() {
while (Serial.available() > 0) {
char t = Serial.read();
}
}
Processing:
import processing.serial.*;
import controlP5.*;
Serial myPort;
ControlP5 cp5;
int slider1 = 0;
int slider2 = 0;
int slider3 = 0;
void setup() {
size(800, 800);
cp5 = new ControlP5(this);
PFont roboto = createFont("Roboto-Bold.ttf", 1, true);
ControlFont font = new ControlFont(roboto, 28);
Controller Aslider1 = cp5.addSlider("slider1")
.setPosition(85, 100)
.setCaptionLabel("Red")
.setRange(0, 255)
.setWidth(191)
.setHeight(50);
cp5.getController("slider1").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
cp5.getController("slider1").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
Controller Aslider2 = cp5.addSlider("slider2")
.setPosition(301, 100)
.setCaptionLabel("Green")
.setRange(0, 255)
.setWidth(191)
.setHeight(50);
cp5.getController("slider2").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
cp5.getController("slider2").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
Controller Aslider3 = cp5.addSlider("slider3")
.setPosition(517, 100)
.setCaptionLabel("Blue")
.setRange(0, 255)
.setWidth(191)
.setHeight(50);
cp5.getController("slider3").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
cp5.getController("slider3").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
myPort = new Serial(this, "COM4", 9600);
}
void draw() {
background(slider1, slider2, slider3);
fill(255);
stroke(1);
rectMode(CENTER);
rect(width/2, 350, 200, 75);
fill(0);
textSize(32);
text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
if (mousePressed) {
fill(100);
stroke(1);
rectMode(CENTER);
rect(width/2, 350, 200, 75);
fill(0);
textSize(32);
text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
} else {
fill(170);
stroke(1);
rectMode(CENTER);
rect(width/2, 350, 200, 75);
fill(0);
textSize(32);
text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
}
}
}
void mouseReleased() {
if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
fill(100);
stroke(1);
rectMode(CENTER);
rect(width/2, 350, 200, 75);
fill(0);
textSize(32);
text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
myPort.write(str(slider1) + " " + str(slider2) + " " + str(slider3));
println(str(slider1) + " " + str(slider2) + " " + str(slider3));
}
}
Then look at this link:
https://forum.arduino.cc/index.php?topic=634061.0
I am using processing-3.2.2 for audio visualization. I have the code to play the visualiser but I don't know how to save it as a video(.mp4) file.
Here's what I wrote:
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer player;
AudioMetaData meta;
BeatDetect beat;
int r = 200;
float rad = 70;
void setup()
{
size(500, 500);
//size(600, 400);
minim = new Minim(this);
player = minim.loadFile("son_final.mp3");
meta = player.getMetaData();
beat = new BeatDetect();
player.play();
//player.play();
background(-1);
noCursor();
}
void draw()
{
float t = map(mouseX, 0, width, 0, 1);
beat.detect(player.mix);
fill(#1A1F18,50);
noStroke();
rect(0, 0, width, height);
translate(width/2, height/2);
noFill();
fill(-2, 10);
if (beat.isOnset()) rad = rad*0.9;
else rad = 70;
ellipse(0, 0, 2*rad, 2*rad);
stroke(-1, 50);
int bsize = player.bufferSize();
for (int i = 0; i < bsize - 1; i+=5)
{
float x = (r)*cos(i*2*PI/bsize);
float y = (r)*sin(i*2*PI/bsize);
float x2 = (r + player.left.get(i)*100)*cos(i*2*PI/bsize);
float y2 = (r + player.left.get(i)*100)*sin(i*2*PI/bsize);
line(x, y, x2, y2);
}
beginShape();
noFill();
stroke(-1, 50);
for (int i = 0; i < bsize; i+=30)
{
float x2 = (r + player.left.get(i)*100)*cos(i*2*PI/bsize);
float y2 = (r + player.left.get(i)*100)*sin(i*2*PI/bsize);
vertex(x2, y2);
pushStyle();
stroke(-5);
strokeWeight(2);
point(x2, y2);
popStyle();
}
endShape();
if (flag) showMeta();
}
void showMeta() {
int time = meta.length();
textSize(50);
textAlign(CENTER);
text( (int)(time/1000-millis()/1000)/60 + ":"+ (time/1000-millis()/1000)%60, -7, 21);
}
boolean flag =false;
void mousePressed() {
if (dist(mouseX, mouseY, width/2, height/2)<150) flag =!flag;
}
void keyPressed() {
if(key=='e')exit();
}
I know I can import MovieMaker library but I don't know how to use it in this code. Please suggest some changes.
Thank you.
You can use the Video Export library for that. More info here: http://funprogramming.org/VideoExport-for-Processing/
Then all you'd do is create an instance of VideoExport and then call videoExport.saveFrame() at the end of your draw() function.
import com.hamoid.*;
VideoExport videoExport;
void setup() {
size(600, 600);
videoExport = new VideoExport(this, "basic.mp4");
}
void draw() {
background(#224488);
rect(frameCount * frameCount % width, 0, 40, height);
videoExport.saveFrame();
}
There are a ton of resources online. I recommend googling "Processing video export" for a ton of results. Then try something out and post an MCVE (not your entire project) showing exactly where you're stuck.
I have been working on the piece of code below in Processing using the sound library Minim, and have been trying to stop the audio recorded into the program from chopping the sound, rendering it somewhat unaudible.
Setup():
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim sound;
AudioOutput speak;
AudioInput mic;
Sampler samp;
MultiChannelBuffer bigBuf;
AudioPlayer loadAudio;
slider frequency, amplitude;
button mute, record, play, stop;
trigger freqBut, ampBut, load;
float[] mainLeft = new float[1024];
float[] mainRight = new float[1024];
float amp = 1;
boolean muted = false, recording = false, playInit = false;
int sampleCount = 0;
int tempCount = 0;
int tempFreq = 0;
boolean tempUnder = false;
int tempTimeNew = 0;
int tempTimeOld = 0;
void setup() {
size(512, 300);
// UI
frequency = new slider(10, 60, 300, 20, 0, 30, true, color(120), color(180), color(255), color(80, 0, 0));
amplitude = new slider(10, 90, 300, 20, 0, 20, false, color(120), color(180), color(255), color(0, 0, 80));
mute = new button(10, 10, 40, 40, color(120), color(180), color(255));
record = new button(60, 10, 40, 40, color(120), color(180), color(255));
play = new button(110, 10, 40, 40, color(120), color(180), color(255));
stop = new button(160, 10, 40, 40, color(120), color(180), color(255));
load = new trigger(210, 10, 40, 40, color(120), color(210), color(180), color(255));
freqBut = new trigger(320, 60, 20, 20, color(40, 0, 0), color(220, 0, 0), color(180, 0, 0), color(255));
ampBut = new trigger(320, 90, 20, 20, color(0, 0, 40), color(0, 0, 220), color(0, 0, 180), color(255));
// Minim
sound = new Minim(this);
mic = sound.getLineIn();
speak = sound.getLineOut(sound.STEREO, mic.bufferSize(), mic.sampleRate());
loadAudio = sound.loadFile("sound.mp3");
loadAudio.loop();
loadAudio.mute();
bigBuf = new MultiChannelBuffer(mic.bufferSize(), 2);
samp = new Sampler(bigBuf, mic.sampleRate(), 2);
samp.patch(speak);
}
And draw():
void draw() {
float[] micLeft;
float[] micRight;
micLeft = mic.left.toArray();
micRight = mic.right.toArray();
if (record.state == true) {
recording = !recording;
}
if (recording == true) {
int temp = mainLeft.length - 1;
mainLeft = expand(mainLeft, temp + micLeft.length);
mainRight = expand(mainRight, temp + micRight.length);
sampleCount++;
for (int i = 0; i < micLeft.length - 1; i++) {
mainLeft[i + temp] = micLeft[i];
mainRight[i + temp] = micRight[i];
}
}
// Play
if (play.state == true) {
playInit = true;
}
if (playInit == true) {
println("playing");
if (tempTimeOld > tempTimeNew) {
tempUnder = true;
}
tempTimeOld = tempTimeNew;
tempTimeNew = millis() % micLeft.length;
println(millis() % (micLeft.length));
if (tempUnder == true) {
if (tempCount == sampleCount) {
playInit = false;
tempCount = 0;
}
else {
// Amplitude
if (ampBut.state == true) {
amp = map(amplitude.value, amplitude.minVal, amplitude.maxVal, 0.05, 20);
}
else {
amp = 1;
}
if (freqBut.state == true) {
float newFreq = map(frequency.value, frequency.minVal, frequency.maxVal, 140, 940);
tempFreq = 0;
for (int i = 0; i < micLeft.length - 2; i++) {
if (micLeft[i] < micLeft[i + 1]) {
tempFreq ++;
}
}
for (int i = 0; i < micLeft.length - 1; i++) {
bigBuf.setSample(0, i, mainLeft[int(i * newFreq / tempFreq) + (tempCount * micLeft.length)] * amp);
bigBuf.setSample(1, i, mainRight[int(i * newFreq / tempFreq) + (tempCount * micRight.length)] * amp);
}
}
else {
for (int i = 0; i < micLeft.length - 1; i++) {
bigBuf.setSample(0, i, mainLeft[i + (tempCount * micLeft.length)] * amp);
bigBuf.setSample(1, i, mainRight[i + (tempCount * micRight.length)] * amp);
}
}
samp.trigger();
tempCount++;
}
}
}
}
Any advise on solving this problem would be a really big help.
And sorry if it have not been asking to right way, it is my first question, and the guide confused me a little.