This site is supported by donations to The OEIS Foundation.
File:Seq.java
From OeisWiki
Seq.java (file size: 8 KB, MIME type: unknown/unknown)
Warning: This file type may contain malicious code.
By executing it, your system may be compromised.
This program plays out sequences as DirectSound (not MIDI) to your sound card; numbers are "mod"-ed to the range -128..127
Code (2011-01-15T18:30:57)
// This program plays out sequences as DirectSound (not MIDI) to your soundcard; // numbers are "mod"-ed to the range -128..127 // As usual: // - no guarantees // - no commercial uses // Have the Sequences file from oeis.org uncompressed at the same directory where you will // run this program; save it as "Sequences" no extensions // Compile and run: // - Start to play a new randomly chosen sequence // - Stop to stop it // - Save to stop and save what you heard through your soundcard (so don't play any other sounds on your computer during this interval or it will be mixed in) // it will be saved as a .wav file // - End to end the program // - if you want to hear pulses instead of buzz add some delay in the delay field (e.g., 1000) // Let me know if you find something interesting (gpaschos /at/ netscape /./ net) import javax.sound.sampled.*; import java.io.*; import java.awt.event.*; import java.awt.*; import java.util.*; public class Seq extends Frame { boolean stopCapture=false, ended=false, noshow=false, pause=false, capturing=false, save=false, stop=false; int k=0, delay=0; String exf=null, m=null; File fo=null; CaptureThread cpt=null; public Seq() { setUp(); BufferedReader inputS=null; try { inputS=new BufferedReader(new FileReader(System.getProperty("user.dir")+File.separatorChar+"Sequences")); String line=inputS.readLine(); k=0; while(line!=null) { k++; line=inputS.readLine(); } inputS.close(); } catch(Exception e) { System.out.println(e.getMessage()+" error reading from file"); } } public void setUp() { setLayout(new FlowLayout()); setSize(200, 100); setLocation(55, 55); ListenerThread lt=new ListenerThread(); lt.start(); if(!noshow) { Button b=new Button("Start"); b.addActionListener(lt); add(b); Button sv=new Button("Save"); sv.addActionListener(lt); add(sv); Button sb=new Button("Stop"); sb.addActionListener(lt); add(sb); Button ebt=new Button("End"); ebt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); add(ebt); Label l=new Label("delay"); add(l); TextField tx=new TextField(" "); tx.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ delay=Integer.parseInt(((TextField)e.getSource()).getText().trim()); } } ); add(tx); setVisible(true); } } public void seq() { stopCapture=false; ended=false; capturing=false; save=false; stop=false; int i, j; long[] ii=null; String sq=null; BufferedReader inputS=null; try { inputS=new BufferedReader(new FileReader(new File(System.getProperty("user.dir")).getParent()+File.separatorChar+"Sequences")); i=(int)(Math.random()*k); j=0; String line=null; while(j++<i-1) line=inputS.readLine(); line=inputS.readLine(); System.out.println("read "+line); StringTokenizer st=new StringTokenizer(line.trim(), ",", false); int c=st.countTokens(); // System.out.println("read "+c); ii=new long[c-1]; sq=st.nextToken().trim(); // System.out.println("read "+sq); for(i=1; i<c; i++) { ii[i-1]=Long.parseLong(st.nextToken()); // System.out.println("read "+ii[i-1]); } inputS.close(); } catch(Exception e) { System.out.println(e.getMessage()+" error reading from file"); } startCapture(); fo=new File(sq+"-"+(System.currentTimeMillis()/1000)+".wav"); SourceDataLine line=null; line=(SourceDataLine)getSourceLineFromMixer(); try { if(line!=null) { line.open(line.getFormat()); line.start(); int chunk=1024; byte[] data=new byte[chunk]; System.out.println("delay "+delay); for(int k1=0; k1<100; k1++) { long[] b=new long[data.length]; for(i=0; i<data.length; i++) b[i]=ii[i%ii.length]; toByte(b, data); note(line, data); if(stop) break; try { Thread.sleep((long)(Math.random()*delay)); } catch(InterruptedException e) { } } line.stop(); line.close(); } } catch(Exception ex) { ex.printStackTrace(); System.out.println("audio problem "+ex); } // System.exit(0); } public void startCapture() { capturing=true; cpt=new CaptureThread(); cpt.start(); } class CaptureThread extends Thread { ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); AudioFormat af=getAudioFormat(); public void run() { TargetDataLine line=null; DataLine.Info info=new DataLine.Info(TargetDataLine.class, af); // format is an AudioFormat object try { line=(TargetDataLine)AudioSystem.getLine(info); line.open(line.getFormat()); } catch(Exception e) { System.out.println("E: "+e); } line.start(); byte[] data=new byte[line.getBufferSize()/5]; int cnt=0; while(!stopCapture && (cnt=line.read(data, 0, data.length))!=-1) { outputStream.write(data, 0, cnt); } try { outputStream.close(); } catch(IOException e) { System.out.println("E: "+e); } if(save) writeWav(outputStream); while(!stopCapture) try { Thread.sleep(10); } catch(InterruptedException e) { } line.close(); ended=true; capturing=false; } void writeWav(ByteArrayOutputStream outputStream) { byte audioData[] = outputStream.toByteArray(); try { InputStream byteArrayInputStream=new ByteArrayInputStream(audioData); AudioInputStream audioInputStream=new AudioInputStream(byteArrayInputStream, af, audioData.length/af.getFrameSize()); AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, fo); } catch(Exception ex) { System.out.println("\nsaving fault 1"); } } }//CaptureThread public SourceDataLine getSourceLineFromMixer() { Mixer.Info[] aInfos=AudioSystem.getMixerInfo(); Line l=null; for(int i=0; i<aInfos.length; i++) { Mixer mixer = AudioSystem.getMixer(aInfos[i]); try { DataLine.Info dinfo=new DataLine.Info(SourceDataLine.class, new AudioFormat(44100.0F, 16, 2, true, true)); l=mixer.getLine(dinfo); if(l!=null) { //System.out.println("l "+l.toString()); break; } } catch(Exception ex) { } } return (SourceDataLine)l; } public void note(SourceDataLine line, byte[] data) { try { for(int k1=0; k1<1; k1++) { int j=0; int k=10+(int)(Math.random()*10); while(j<k) { line.write(data, 0, data.length); j++; } } } catch(Exception ex) { ex.printStackTrace(); System.out.println("audio problem "+ex); } } public void toByte(long[] v, byte[] b) { int i; for(i=0; i<v.length; i++) b[i]=(byte)((((long)v[i]/128)%2)*(-128)+(v[i]%128)); } AudioFormat getAudioFormat() { float sampleRate = 44100F; //8000,11025,16000,22050,44100 int sampleSizeInBits = 16; //8,16 int channels = 2; //1,2 boolean signed = true; //true,false boolean bigEndian = true; //true,false return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); } class ListenerThread extends Thread implements ActionListener { public void actionPerformed(ActionEvent e) { Object o=e.getSource(); if(o instanceof Button) { if(((Button)o).getLabel().equals("Start")) { Thread t=new Thread() { public void run() { seq(); } }; t.start(); } else if(((Button)o).getLabel().equals("Stop")) { save=false; stop=true; stopCapture=true; cpt=null; } else if(((Button)o).getLabel().equals("Save")) { save=true; stop=true; stopCapture=true; while(capturing && !ended) { try { Thread.sleep(10); } catch(InterruptedException ex) { } } cpt=null; } } else if(o instanceof TextField) { } } public void run() { while(true) try { Thread.sleep(10); } catch(InterruptedException ex) { } } } public static void main(String[] args) { Seq s=new Seq(); // s.seq(); }//main() }
File history
Click on a date/time to view the file as it appeared at that time.
Date/Time | Dimensions | User | Comment | |
---|---|---|---|---|
current | 13:30, 15 January 2011 | (8 KB) | George Paschos (talk | contribs) | This program plays out sequences as direct sound (not midi) to your sound card; numbers are "mod"-ed to the range -128..127 |
- You cannot overwrite this file.
File usage
The following page links to this file: