summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--android/src/org/rockbox/RockboxPCM.java51
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
24import java.util.Arrays; 24import java.util.Arrays;
25 25
26import android.content.Context;
26import android.content.Intent; 27import android.content.Intent;
27import android.media.AudioFormat; 28import android.media.AudioFormat;
28import android.media.AudioManager; 29import 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);