Sound layering issue
in
Core Library Questions
•
1 years ago
I've set this program to play audio files of different cricket chirp frequencies depending on real time temperature pulled from yahoo, but seem to have a (layering)? problem because it sounds like it is playing all of the audio files at once. Any suggestions?
//Parsing Yahoo's XML weather feed manually
import processing.video.*;
Movie movie;
PFont f;
//a zip code from each US state capital
String[] zips = { "36101" , "99801", "85001", "72201", "94203", "80201", "06101", "19901", "32301", "30301", "96801", "83701", "62701", "46201", "50301", "66601", "40601", "70801", "04330", "21401", "02108", "48901", "55101", "39201", "65101", "59601", "68501", "89701", "03301", "08601", "87501", "12201", "27601", "58501", "43201", "73101", "97301", "17101", "02901", "29201", "57501", "37201", "73301", "84101", "05601", "23218", "98501", "25301", "53701", "82001" };
int counter = 0;
// The WeatherGrabber object does the work for us!
WeatherGrabber wg;
void setup() {
size(200,200);
wg = new WeatherGrabber(zips[counter]); // Make a WeatherGrabber object
wg.requestWeather(); // Tell it to request the weather
f = createFont( "AmericanTypewriter",16,true);
}
void draw() {
background(100);
textFont(f);
fill(0);
// Get the values to display
String weather = wg.getWeather();
int temp = wg.getTemp();
// call up audio files based on temp retrieved
if (wg.temperature >= 100) {
movie = new Movie (this, "Cricket 1everyhalfsec.m4a"); //call the cricket sound
movie.stop();
} else if (wg.temperature >= 90 || wg.temperature < 100) {
movie = new Movie (this, "Cricket 1_sec.m4a");
movie.stop();
} else if (wg.temperature >= 80 || wg.temperature < 90) {
movie = new Movie(this, "Cricket 1every2sec.m4a");
movie.stop();
} else if (wg.temperature >= 70 || wg.temperature < 80) {
movie = new Movie(this, "Cricket 1every3sec.m4a");
movie.stop();
} else if (wg.temperature >= 60 || wg.temperature < 70) {
movie = new Movie(this, "Cricket 1every4sec.m4a");
movie.stop();
} else if (wg.temperature >= 50 || wg.temperature < 60) {
movie = new Movie(this, "Cricket 1every5sec.m4a");
movie.stop();
} else if (wg.temperature >= 40 || wg.temperature < 50) {
movie = new Movie(this, "Cricket 1every6sec.m4a");
movie.stop();
} else if (wg.temperature >= 30 || wg.temperature < 40) {
movie = new Movie(this, "Cricket 1every7sec.m4a");
movie.stop();
} else if (wg.temperature >= 20 || wg.temperature < 30) {
movie = new Movie(this, "Cricket 1every8sec.m4a");
movie.stop();
} else if (wg.temperature >= 10 || wg.temperature < 20) {
movie = new Movie(this, "Cricket 1every9sec.m4a");
movie.stop();
} else {
movie = new Movie(this, "Cricket 1every10sec.m4a");
}
// Display all the stuff we want to display
text(zips[counter],10,160);
text(weather,10,90);
text(temp,10,40);
//text("Click to change zip. " ,10,180);
// Draw a little thermometer based on the temperature
stroke(0);
fill(175);
rect(10,50,temp*2,20);
}
void mousePressed() {
// Increment the counter and get the weather at the next zip code
counter = (counter + 1) % zips.length;
wg.setZip(zips[counter]);
// The data is requested again with a new zip code every time the mouse is pressed.
wg.requestWeather();
// movie.stop();
// movie.play();
}
class WeatherGrabber {
int temperature = 0;
String weather = "";
String zip;
WeatherGrabber(String tempZip) {
zip = tempZip;
}
// Set a new Zip code
void setZip(String tempZip) {
zip = tempZip;
}
// Get the temperature
int getTemp() {
return temperature;
}
// Get the weather
String getWeather() {
return weather;
}
// Make the actual XML request
void requestWeather() {
// Get all the HTML/XML source code into an array of strings
// (each line is one element in the array)
String url = "http://xml.weather.yahoo.com/forecastrss?p=" + zip;
String[] lines = loadStrings(url);
// Turn array into one long String
String xml = join(lines, "" );
// Searching for weather condition
String lookfor = "<yweather:condition text=\"";
String end = "\"";
weather = giveMeTextBetween(xml,lookfor,end);
// Searching for temperature
lookfor = "temp=\"";
temperature = int(giveMeTextBetween (xml,lookfor,end));
}
// A function that returns a substring between two substrings
String giveMeTextBetween(String s, String before, String after) {
String found = "";
int start = s.indexOf(before); // Find the index of the beginning tag
if (start == - 1) return""; // If we don't find anything, send back a blank String
start += before.length(); // Move to the end of the beginning tag
int end = s.indexOf(after,start); // Find the index of the end tag
if (end == -1) return""; // If we don't find the end tag, send back a blank String
return s.substring(start,end); // Return the text in between
}
}
1