Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: Java and wav file generation Date: Thu, 06 Oct 2011 16:46:23 -0700 Organization: A noiseless patient Spider Lines: 556 Message-ID: References: <47c5d2bb-222a-4d1b-96a4-aeb915f861f5@h10g2000yqd.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 6 Oct 2011 23:46:16 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="25651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18pi0t0lxH6XpsslB4rnJ/m" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 In-Reply-To: <47c5d2bb-222a-4d1b-96a4-aeb915f861f5@h10g2000yqd.googlegroups.com> Cancel-Lock: sha1:3ZctJ1cI8iJBwU2XWsUce3o4Ti4= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8610 On 10/6/2011 11:16 AM, Zio Peppe wrote: > Hi folks, > > I have a set of binary data of which are known in advance the size > (number of points), the format (32-bit long integers), the sample rate > and the endian (Big or Little). > > I would like to build audio files (wav or mp3, doesn't matter for now) > from them with absolutely high fidelity. > > I was wondering if there is already a class suitable for such > processing and some example of use. > > I had a look on Package javax.sound.sampled, but it is a bit > complicated for a beginner as me :/ > > Is there a good soul who can show with a bit of code how I can get > success? > > Cheers, > ZPP These might be of some help. They are just snippets used for testing things but you might be able to make something of it. Post back once you have better defined what you are up to. import java.io.*; import javax.sound.sampled.*; public class Play { public static void main(String[] args) { class MyLineListener implements LineListener { public void update(LineEvent le) { LineEvent.Type type = le.getType(); System.out.println(type); } }; try { AudioInputStream fis = AudioSystem.getAudioInputStream(new File(args[0])); System.out.println("Input AudioFormat: " + fis.getFormat()); AudioInputStream ais = AudioSystem.getAudioInputStream( AudioFormat.Encoding.PCM_SIGNED,fis); AudioFormat af = ais.getFormat(); System.out.println("Output AudioFormat: " + af.toString()); /* int frameRate = (int)af.getFrameRate(); System.out.println("Frame Rate: " + frameRate); int frameSize = af.getFrameSize(); System.out.println("Frame Size: " + frameSize); */ SourceDataLine line = AudioSystem.getSourceDataLine(af); line.addLineListener(new MyLineListener()); line.open(af); int bufSize = line.getBufferSize(); System.out.println("Buffer Size: " + bufSize); line.start(); byte[] data = new byte[bufSize]; int bytesRead; while ((bytesRead = ais.read(data,0,data.length)) != -1) line.write(data,0,bytesRead); line.drain(); line.stop(); line.close(); } catch (Exception e) { System.out.println(e); } } } import java.io.*; import java.net.*; import javax.sound.sampled.*; public class PlayClip { public static void main(String[] args) { try { AudioInputStream ais = AudioSystem.getAudioInputStream(new File(args[0])); // AudioSystem.getAudioInputStream(new URL("http://pscode.org/media/leftright.wav")); AudioFormat af = ais.getFormat(); Clip line = AudioSystem.getClip(); line.open(ais); line.start(); Thread.sleep(1); line.drain(); line.stop(); line.close(); } catch (Exception e) { System.out.println(e); } } } import javax.sound.sampled.*; public class Mixers { public static void main(String[] args) { Mixer.Info[] mi = AudioSystem.getMixerInfo(); for (int i=0; i 1.0 || vol < 0.0) throw new IllegalArgumentException("Volume out of range 0.0 - 1.0"); byte[] buf = new byte[(int)SAMPLE_RATE * msecs / 1000]; for (int i=0; i