Sound gets distorted when runned in processing

edited January 2018 in Arduino

Hello World, I have written a processing code and have implemented a sound file. When I play it on a common player, a cleat woman voice comes out. If I run my code processing is distorting her voice into a slow man version... I am runnin processing on a MacBook Pro with the latest Java version.

import processing.serial.*;
import processing.sound.*;


int S1_WAITING_FOR_PEOPLE  = 1;
int S2_PEOPLE_ENTERED      = 2;
int S3_LIGHT_FADE_OUT      = 3;
int S4_DARK                = 4;
int S5_MUSIC               = 5;
int S6_LIGHT_FADE_IN       = 6;
// make sure this is the last!!!!
int TERMINATE              = 7;

int mode = S1_WAITING_FOR_PEOPLE;




int no_one_when_distance_greater_then = 160; // cm
int time_before_dim = 5000; // ms

SoundFile file;
boolean is_playing = false;

Serial myPort;  

int dist;

boolean someone_present = false;
int someone_present_since_time;


int music_playing_since;
int music_duration;

int in_mode_since_ms;

void setup() {
  size(640, 360);

  printArray(Serial.list()); // "/dev/cu.usbmodem1411" cu.usb!!!

  myPort = new Serial(this, Serial.list()[1], 9600);
  //myPort.readStringUntil('\n');
  myPort.bufferUntil('\n');

  // Load a soundfile from the data folder of the sketch and play it back in a loop
  file = new SoundFile(this, "03.mp3");
  //file.loop();
  music_duration = (int) file.duration() * 1000;
  music_duration += 2000;

}      

void draw() {
  background(0);
  fill(255);
  text("frameCount: "+frameCount, 50, 25);
  text("dist: "+dist, 50, 50);
  text("mode: "+mode, 50, 75);


  if (frameCount == 180) {
    turn_on();
  } else if (frameCount > 180) {

    if ( mode == S1_WAITING_FOR_PEOPLE) {

      if (someone_present == false) {
        if (dist < no_one_when_distance_greater_then) {
          someone_present = true;
          someone_present_since_time = millis();
          change_mode(mode + 1);
        }
      }
    } else if (mode == S2_PEOPLE_ENTERED) {
      text("millis: "+millis(), 50, 100);
      text("someone_present_since_time: "+someone_present_since_time, 50, 125);
      text("time_before_dim: "+time_before_dim, 50, 150);
      if (millis() - someone_present_since_time > time_before_dim) {
        change_mode(mode + 1);
      }
    } else if (mode == S3_LIGHT_FADE_OUT) {
      turn_off();
      change_mode(mode + 1);
    } else if (mode == S4_DARK) 
    {
      change_mode(mode + 1);
    } else if (mode == S5_MUSIC) 
    {
      if (is_playing == false) {
        file.play();
        is_playing = true;
        music_playing_since = millis();
      }
      if (millis() - music_playing_since > music_duration) {
        is_playing = false;
        file.stop();
        change_mode(mode + 1);
      }
    } else if (mode == S6_LIGHT_FADE_IN) 
    {
      // todo
      if (millis() - in_mode_since_ms > 1000) {
        change_mode(mode + 1);
      }

    } else if (mode == TERMINATE) 
    {
      change_mode(S1_WAITING_FOR_PEOPLE);
      someone_present = false; // reset
      println("turn_on();");
      turn_on();
    }
  }
}


void change_mode(int m) {
  mode = m;
  in_mode_since_ms = millis();
}



void serialEvent(Serial p) { 
  String s = p.readString();
  if (s != null) {
    s = s.replace("\r\n", "");
    if (s.contains("Distance Measured")) {
      String[] tokens = split(s, "=");
      dist = int(tokens[1]);
    }
  }
} 


void turn_on() {
  myPort.write("on");
}

void turn_off() {
  myPort.write("off");
}

void keyPressed() {
  if (key == 'a') {
    turn_on();
  }
  if (key == 's') {
    turn_off();
  }
}

Thanks for your help!

Answers

  • I have fixed it!

  • @Faffie -- so glad that you found a solution. How did you fix it? Your answer might help other people who have a similar problem.

  • Thank you! Oh, sure! I have put the audio into:

    Minim minim; AudioPlayer sound;

    Then I have made an mp3 from it and it worked properly :)

Sign In or Register to comment.