Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java equivelant to AS3's sound.extract()?  (Read 2117 times)
0 Members and 1 Guest are viewing this topic.
Offline eedok

Junior Newbie





« Posted 2009-09-01 21:15:22 »

I'm trying to port some Actionscript code to Java, and I'm looking for a way to get an array of frequencies in a format where it's 1 float per sample per channel and there's 2 channels from an mp3 file.

Anyone have an example available on how to accomplish this?
Offline erikd

JGO Ninja


Medals: 15
Projects: 4
Exp: 14 years


Maximumisness


« Reply #1 - Posted 2009-09-02 11:08:03 »

Do you really mean frequencies or just an array of samples?
Guessing you mean samples, check the documentation of JLayer (an mp3 decoder library) at http://www.javazoom.net/javalayer/documents.html
I never used JLayer myself, but I'm sure you can figure out how to get the decoded audio data from your mp3 using JLayer using the docs.

Offline eedok

Junior Newbie





« Reply #2 - Posted 2009-09-03 00:19:19 »

hrmm I'll look into it, but right now I've got this working:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
import java.io.*;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;


public class JMFTest {

   public static void main(String[] args) throws Exception {
       FileInputStream in = new FileInputStream("test.mp3");
       AudioInputStream source = AudioSystem.getAudioInputStream(new BufferedInputStream(in, 1024));
       AudioInputStream pcm = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, source);
       File newFile = new File("test.wav");
       AudioSystem.write(pcm, AudioFileFormat.Type.WAVE, newFile);
       FileInputStream fis = new FileInputStream(newFile);
       ByteArrayOutputStream boas = new ByteArrayOutputStream(new Long(newFile.length()).intValue());
       byte[] buf = new byte[8 * 1024];
       for(int len=0; (len = fis.read(buf)) != -1;)
       {
          boas.write(buf,0,len);
       }
       byte[] ba = boas.toByteArray();
       for(int i = 44; i < ba.length; i+=2)
       {
          float value = ((ba[i] & 0xFF) | (ba[i + 1] << 8))/ 32768.0F;
          value /= 2.0;
          ba[i + 1] = (byte)((int)(value * 32768) >> 8);
          ba[i] = (byte)((int)(value * 32768) & 0xFF);
       }
       System.out.println(ba.length - 44);
       File newerFile = new File("test1.wav");
       FileOutputStream fos = new FileOutputStream(newerFile);
       fos.write(ba);
    }
}

right now it halves the volume of the sound file, but only seems to work in the format I want if the mp3 used is in the same format, so I'm looking for a simpler and more consistent way to do this, I'll look into JLayer, and see if I can find what I'm looking for.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mrbungle

Senior Newbie


Projects: 1



« Reply #3 - Posted 2009-09-13 15:45:20 »

I'm trying to port some Actionscript code to Java, and I'm looking for a way to get an array of frequencies in a format where it's 1 float per sample per channel and there's 2 channels from an mp3 file.

Anyone have an example available on how to accomplish this?

(Fast) fourier transform will give you the spectrum. There's plenty of Java FFT implementations around.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (247 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!