diff options
author | Björn Stenberg <bjorn@haxx.se> | 2011-03-11 20:40:19 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2011-03-11 20:40:19 +0000 |
commit | 00f75c92bf4f9b04cd919ef7f61ca68ddc93f94b (patch) | |
tree | f09f615718e97f026925b49e2563f11936c35ac0 | |
parent | 0b18522cd09921995d130bd451d8288162ea68d9 (diff) | |
download | rockbox-00f75c92bf4f9b04cd919ef7f61ca68ddc93f94b.tar.gz rockbox-00f75c92bf4f9b04cd919ef7f61ca68ddc93f94b.zip |
Use the pcm volume to add finer steps between every android stream volume step.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29573 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | android/src/org/rockbox/RockboxPCM.java | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/android/src/org/rockbox/RockboxPCM.java b/android/src/org/rockbox/RockboxPCM.java index 252fcf6802..5a8354abee 100644 --- a/android/src/org/rockbox/RockboxPCM.java +++ b/android/src/org/rockbox/RockboxPCM.java | |||
@@ -23,6 +23,7 @@ package org.rockbox; | |||
23 | 23 | ||
24 | import java.util.Arrays; | 24 | import java.util.Arrays; |
25 | 25 | ||
26 | import android.content.Context; | ||
26 | import android.content.Intent; | 27 | import android.content.Intent; |
27 | import android.media.AudioFormat; | 28 | import android.media.AudioFormat; |
28 | import android.media.AudioManager; | 29 | import android.media.AudioManager; |
@@ -38,6 +39,7 @@ public class RockboxPCM extends AudioTrack | |||
38 | private PCMListener l; | 39 | private PCMListener l; |
39 | private HandlerThread ht; | 40 | private HandlerThread ht; |
40 | private Handler h = null; | 41 | private Handler h = null; |
42 | private static final int streamtype = AudioManager.STREAM_MUSIC; | ||
41 | private static final int samplerate = 44100; | 43 | private static final int samplerate = 44100; |
42 | /* should be CHANNEL_OUT_STEREO in 2.0 and above */ | 44 | /* should be CHANNEL_OUT_STEREO in 2.0 and above */ |
43 | private static final int channels = | 45 | private static final int channels = |
@@ -48,6 +50,11 @@ public class RockboxPCM extends AudioTrack | |||
48 | private static final int buf_len = | 50 | private static final int buf_len = |
49 | Math.max(24<<10, getMinBufferSize(samplerate, channels, encoding)); | 51 | Math.max(24<<10, getMinBufferSize(samplerate, channels, encoding)); |
50 | 52 | ||
53 | private AudioManager audiomanager; | ||
54 | private int maxstreamvolume; | ||
55 | private float minpcmvolume; | ||
56 | private float pcmrange; | ||
57 | |||
51 | private void LOG(CharSequence text) | 58 | private void LOG(CharSequence text) |
52 | { | 59 | { |
53 | Log.d("Rockbox", (String) text); | 60 | Log.d("Rockbox", (String) text); |
@@ -55,7 +62,7 @@ public class RockboxPCM extends AudioTrack | |||
55 | 62 | ||
56 | public RockboxPCM() | 63 | public RockboxPCM() |
57 | { | 64 | { |
58 | super(AudioManager.STREAM_MUSIC, samplerate, channels, encoding, | 65 | super(streamtype, samplerate, channels, encoding, |
59 | buf_len, AudioTrack.MODE_STREAM); | 66 | buf_len, AudioTrack.MODE_STREAM); |
60 | ht = new HandlerThread("audio thread", | 67 | ht = new HandlerThread("audio thread", |
61 | Process.THREAD_PRIORITY_URGENT_AUDIO); | 68 | Process.THREAD_PRIORITY_URGENT_AUDIO); |
@@ -63,6 +70,15 @@ public class RockboxPCM extends AudioTrack | |||
63 | raw_data = new byte[buf_len]; /* in shorts */ | 70 | raw_data = new byte[buf_len]; /* in shorts */ |
64 | Arrays.fill(raw_data, (byte) 0); | 71 | Arrays.fill(raw_data, (byte) 0); |
65 | l = new PCMListener(buf_len); | 72 | l = new PCMListener(buf_len); |
73 | |||
74 | /* find cleaner way to get context? */ | ||
75 | final RockboxService rb = RockboxService.get_instance(); | ||
76 | audiomanager = | ||
77 | (AudioManager) rb.getSystemService(Context.AUDIO_SERVICE); | ||
78 | maxstreamvolume = audiomanager.getStreamMaxVolume(streamtype); | ||
79 | |||
80 | minpcmvolume = getMinVolume(); | ||
81 | pcmrange = getMaxVolume() - minpcmvolume; | ||
66 | } | 82 | } |
67 | 83 | ||
68 | private int bytes2frames(int bytes) | 84 | private int bytes2frames(int bytes) |
@@ -130,19 +146,26 @@ public class RockboxPCM extends AudioTrack | |||
130 | 146 | ||
131 | private void set_volume(int volume) | 147 | private void set_volume(int volume) |
132 | { | 148 | { |
133 | /* volume comes from 0..-990 from Rockbox */ | 149 | /* Rockbox 'volume' is 0..-990 deci-dB attenuation. |
134 | /* TODO: | 150 | Android streams have rather low resolution volume control, |
135 | * volume is in dB, but this code acts as if it were in %, convert? */ | 151 | typically 8 or 15 steps. |
136 | float fvolume; | 152 | Therefore we use the pcm volume to add finer steps between |
137 | /* special case min and max volume to not suffer from | 153 | every android stream volume step. |
138 | * floating point accuracy */ | 154 | It's not "real" dB, but it gives us 100 volume steps. |
139 | if (volume == 0) | 155 | */ |
140 | fvolume = 1.0f; | 156 | |
141 | else if (volume == -990) | 157 | float fraction = 1 - (volume / -990.0f); |
142 | fvolume = 0.0f; | 158 | int streamvolume = (int)Math.ceil(maxstreamvolume * fraction); |
143 | else | 159 | if (streamvolume > 0) { |
144 | fvolume = (volume + 990)/990.0f; | 160 | float streamfraction = (float)streamvolume / maxstreamvolume; |
145 | setStereoVolume(fvolume, fvolume); | 161 | float pcmvolume = |
162 | (fraction / streamfraction) * pcmrange + minpcmvolume; | ||
163 | setStereoVolume(pcmvolume, pcmvolume); | ||
164 | } | ||
165 | |||
166 | int oldstreamvolume = audiomanager.getStreamVolume(streamtype); | ||
167 | if (streamvolume != oldstreamvolume) | ||
168 | audiomanager.setStreamVolume(streamtype, streamvolume, 0); | ||
146 | } | 169 | } |
147 | 170 | ||
148 | public native void pcmSamplesToByteArray(byte[] dest); | 171 | public native void pcmSamplesToByteArray(byte[] dest); |