Line | |
---|
1 | package org.apollo.util;
|
---|
2 |
|
---|
3 | import javax.sound.sampled.AudioFormat;
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * Misc audio routines.
|
---|
7 | *
|
---|
8 | * @author Brook Novak
|
---|
9 | *
|
---|
10 | */
|
---|
11 | public class AudioMath {
|
---|
12 | private AudioMath() {}
|
---|
13 |
|
---|
14 | public static float volToDb(float volume) {
|
---|
15 | return (float) (Math.log(volume==0.0 ? 0.0001 : volume) / Math.log(10.0) * 20.0);
|
---|
16 | }
|
---|
17 |
|
---|
18 | public static float dBToVol(float dB) {
|
---|
19 | return (float) Math.pow(10.0, dB / 20.0);
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | *
|
---|
24 | * @param ms
|
---|
25 | * @param format
|
---|
26 | * @return How many frames are in the given ms time
|
---|
27 | */
|
---|
28 | public static int millisecondsToFrames(long ms, AudioFormat format) {
|
---|
29 | double portion = ms;
|
---|
30 | portion /= 1000.0;
|
---|
31 | return (int)(format.getFrameRate() * portion);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public static long framesToMilliseconds(long frames, AudioFormat format) {
|
---|
35 | double portion = frames;
|
---|
36 | portion /= (double)format.getFrameRate();
|
---|
37 | return (long)(portion * 1000.0);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static int framesToSeconds(long frames, AudioFormat format) {
|
---|
41 | return (int)(frames / format.getFrameRate());
|
---|
42 | }
|
---|
43 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.