FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Tools
(Moderator: REAS)
   writing .WAV files
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: writing .WAV files  (Read 2496 times)
Dimitre

WWW
writing .WAV files
« on: Sep 18th, 2003, 9:31pm »

program to genereate wave files
only runs inside proce55ing
 
Code:
// wave generator
//  
// this sketch generates a file called "tuu.wav" in
// proce55ing root directory.
// experiment values in variables altura and altura2
// to get tune variations
// try opening generated file in your favorite sound editor :)
//
// 17/07/2003
// bona (www.papamuerta.com) & dimitre
 
byte[] valor = new byte[200+88200];
// 88200 = 44100 samples/sec * 2 channels = 1 second of wave
 
int index;
float amplitude;
double sample, sample2;
float altura, altura2, divisor, divisor2;
 
void setup() {
  System.arraycopy(waveHeader, 0, valor, 0, waveHeader.length);
  // volume do som, vai de 0 a 127
  amplitude = 100;
  // frequencia em HZ, 440 é a nota Lá
  altura = 240f;
  altura2 = 1360f;
  
  for (index=0; index<88200; index++) {
    divisor = 88200/altura;
    divisor2 = 88200/altura2;
    // onda normal
    // sample = (sin(2*PI*index/divisor) * amplitude);
    // onda composta
    sample = ((sin(2*PI*index/divisor) * amplitude) + (sin(2*PI*index/divisor2) * amplitude)) / 2;
    
    //tremolo
    //amplitude = (sin(index/1000f) * 127);
    if (sample < 0) sample = 255 - (sample * -1) ;
    valor[waveHeader.length+index] = byte(sample);
  }
  geraWave();  
}
 
void geraWave(){
//File file = null;
//file = new File("tuu.wav");
        try {
            java.io.FileOutputStream fileoutputstream = new FileOutputStream("tuu.wav");
            fileoutputstream.write(valor);
            fileoutputstream.flush();
            fileoutputstream.close();
        }
        catch(IOException ioexception){
            ioexception.printStackTrace();
        }
    }
 
static byte waveHeader[] = {
(byte)0x52, (byte)0x49, (byte)0x46, (byte)0x46, (byte)0x8a, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x57,  
(byte)0x41, (byte)0x56, (byte)0x45, (byte)0x66, (byte)0x6d, (byte)0x74, (byte)0x20, (byte)0x10, (byte)0x00,  
(byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x44, (byte)0xac, (byte)0x00,  
(byte)0x00, (byte)0x88, (byte)0x58, (byte)0x01, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x10, (byte)0x00,  
(byte)0x64, (byte)0x61, (byte)0x74, (byte)0x61, (byte)0x8A, (byte)0x58, (byte)0x01, (byte)0x00
};
« Last Edit: Sep 18th, 2003, 9:33pm by Dimitre »  
Pages: 1 

« Previous topic | Next topic »