Advertisement
Guest User

Untitled

a guest
Jul 17th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.InputStreamReader;
  6.  
  7. import javax.sound.sampled.AudioFormat;
  8.  
  9. import paulscode.sound.SoundSystem;
  10. import paulscode.sound.SoundSystemConfig;
  11. import paulscode.sound.Vector3D;
  12. import paulscode.sound.libraries.LibraryLWJGLOpenAL;
  13.  
  14. public class Main {
  15.  
  16.     public static void main(String[] args) throws Throwable {
  17.         BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in));
  18.         System.out.println("Pausing");
  19.         systemIn.readLine();
  20.        
  21.         float sampleRate = 8000;
  22.         int sampleSizeInBits = 8;
  23.         int channels = 1;
  24.         boolean signed = false;
  25.         boolean bigEndian = false;
  26.  
  27.         System.out.println("Setting up sound system.");
  28.         String sourceName = "TestSource";
  29.        
  30.         SoundSystemConfig.addLibrary(LibraryLWJGLOpenAL.class);
  31.        
  32.         SoundSystem system = new SoundSystem();
  33.        
  34.         system.rawDataStream(format, false, sourceName, 0, 0, 0, SoundSystemConfig.ATTENUATION_NONE, 1f);
  35.        
  36.  
  37.         System.out.println("Reading sound.");
  38.        
  39.         FileInputStream fileIn = new FileInputStream(new File("PATH_TO_SOUND_FILE"));
  40.         ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
  41.        
  42.         byte[] buffer = new byte[1024];
  43.         int read = -1;
  44.        
  45.         while((read = fileIn.read(buffer)) != -1) {
  46.             byte[] data = new byte[read];
  47.             System.arraycopy(buffer, 0, data, 0, read);
  48.            
  49.             system.feedRawAudioData(sourceName, data);
  50.             byteOut.write(data);
  51.         }
  52.         fileIn.close();
  53.         System.out.println("Finished reading sound.");
  54.        
  55.        
  56.         System.out.println("Building buffer of sound.");
  57.         buffer = byteOut.toByteArray();
  58.        
  59.         for(int i=0; i<10; i++) {
  60.             system.feedRawAudioData(sourceName, buffer);
  61.         }
  62.         System.out.println("Finished building buffer.");
  63.        
  64.        
  65.         Thread.sleep(10 * 1000);
  66.         Vector3D pos = system.getListenerData().position.add(new Vector3D(5, 5, 5));
  67.        
  68.         System.out.println("Moving listener to: " + pos);
  69.         system.setListenerPosition(pos.x, pos.y, pos.z);
  70.        
  71.         Thread.sleep(10 * 1000);
  72.        
  73.         System.out.println("Moving source to: " + pos);
  74.         system.setPosition(sourceName, pos.x, pos.y, pos.z);
  75.        
  76.         Thread.sleep(10 * 1000);
  77.        
  78.         pos = pos.add(new Vector3D(-5, -5, -5));
  79.        
  80.         System.out.println("Moving listener back to: " + pos);
  81.         system.setListenerPosition(pos.x, pos.y, pos.z);
  82.        
  83.         Thread.sleep(10 * 1000);
  84.        
  85.         System.out.println("Sound done.");
  86.         system.cleanup();
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement