/********************************************* ETNA SONIFICATION SCALE: YES INTERPOLATION: YES INTERPOLATION METHOD: LINEAR *********************************************/ import javax.sound.sampled.*; import java.io.*; import java.nio.channels.*; import java.nio.*; import java.util.*; public class Sonification { AudioFormat audioFormat; AudioInputStream audioInputStream; SourceDataLine sourceDataLine; float sampleRate = 22050.0F; int sampleSizeInBits = 16; int channels = 1; boolean signed = true; boolean bigEndian = true; double duration=120; //Allaowabe any duration in seconds //A buffer to hold two seconds monaural and one // second stereo data at 16000 samp/sec for // 16-bit samples byte audioData[] = new byte[(int)(sampleRate*duration*2)]; //-------------------------------------------// public static void main(String args[]){ new Sonification(); }//end main //-------------------------------------------// public Sonification(){//constructor new SynGen().getSyntheticData(audioData); playAndStoreData(); }//end constructor private void playAndStoreData() { try{ InputStream byteArrayInputStream = new ByteArrayInputStream(audioData); audioFormat = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); audioInputStream = new AudioInputStream(byteArrayInputStream, audioFormat, audioData.length/audioFormat.getFrameSize()); try{ AudioSystem.write( audioInputStream, AudioFileFormat.Type.AIFF, new File("Sonification"+".aiff")); }catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch }catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch }//end playAndStoreData //=============================================// class SynGen{ ByteBuffer byteBuffer; ShortBuffer shortBuffer; int byteLength; void getSyntheticData(byte[] synDataBuffer){ byteBuffer = ByteBuffer.wrap(synDataBuffer); shortBuffer = byteBuffer.asShortBuffer(); byteLength = synDataBuffer.length; synthesize(); }//end getSyntheticData method void synthesize(){ channels = 1;//Java allows 1 or 2 int bytesPerSamp = 2; sampleRate = 22050.0F; // Allowable 8000,11025,16000,22050,44100 int sampLength = byteLength/bytesPerSamp; double bufferForSpectrum[]; bufferForSpectrum = new double [sampLength]; //BEGIN SONIFICATION PARAMETERS************************************ String filename="etna_tot.dat"; double originalSamplingFreq=100; //DO NOT MODIFY //sonification of geophysical data sampled at 100 Hz double resampleFreq=10; double scalefactor=2; double holdfactor=(sampleRate/originalSamplingFreq)/resampleFreq; //END SONIFICATION PARAMETERS************************************** String aLine = ""; String bLine = ""; double value=0; double value0=0; double max=value; double min=value; double slope=0; double nextvalue=value; int nsamplesHold=(int)holdfactor; try{ //calculate scale factor and normalization int cnt=0; FileInputStream fin = new FileInputStream(filename); BufferedReader myInput = new BufferedReader (new InputStreamReader(fin)); aLine=aLine = myInput.readLine(); try { value = Double.valueOf(aLine.trim()).doubleValue(); //System.out.println("value = " + value); } catch (NumberFormatException nfe) { System.out.println("NumberFormatException: " + nfe.getMessage()); } max=value; min=value; while (((aLine = myInput.readLine()) != null) && (cntmax){ max=value; } else if (value Got next value in buffer = " + nextvalue); nextvalue=nextvalue-(double)(max+min)/(double)2; nextvalue=(double)nextvalue/(double)(max-min); nextvalue=scalefactor*nextvalue; //calculate new slope slope=(nextvalue-value)/holdfactor; } catch (NumberFormatException nfe) { System.out.println("NumberFormatException: " + nfe.getMessage()); } } } } catch( Exception e ) { e.printStackTrace(); }//end catch // Writing Sound datafile as ASCII try{ String outputFileName = "Sound.dat"; FileWriter outputFileReader = new FileWriter(outputFileName); PrintWriter outputStream = new PrintWriter(outputFileReader); for (int a=0; a