|
| 1 | +/* |
| 2 | +Simple DirectMedia Layer |
| 3 | +Java source code (C) 2009-2011 Sergii Pylypenko |
| 4 | + |
| 5 | +This software is provided 'as-is', without any express or implied |
| 6 | +warranty. In no event will the authors be held liable for any damages |
| 7 | +arising from the use of this software. |
| 8 | +
|
| 9 | +Permission is granted to anyone to use this software for any purpose, |
| 10 | +including commercial applications, and to alter it and redistribute it |
| 11 | +freely, subject to the following restrictions: |
| 12 | + |
| 13 | +1. The origin of this software must not be misrepresented; you must not |
| 14 | + claim that you wrote the original software. If you use this software |
| 15 | + in a product, an acknowledgment in the product documentation would be |
| 16 | + appreciated but is not required. |
| 17 | +2. Altered source versions must be plainly marked as such, and must not be |
| 18 | + misrepresented as being the original software. |
| 19 | +3. This notice may not be removed or altered from any source distribution. |
| 20 | +*/ |
| 21 | + |
| 22 | +package org.renpy.android; |
| 23 | + |
| 24 | + |
| 25 | +import android.app.Activity; |
| 26 | +import android.content.Context; |
| 27 | +import android.os.Bundle; |
| 28 | +import android.view.MotionEvent; |
| 29 | +import android.view.KeyEvent; |
| 30 | +import android.view.Window; |
| 31 | +import android.view.WindowManager; |
| 32 | +import android.media.AudioTrack; |
| 33 | +import android.media.AudioManager; |
| 34 | +import android.media.AudioFormat; |
| 35 | +import java.io.*; |
| 36 | +import java.nio.ByteBuffer; |
| 37 | +import android.util.Log; |
| 38 | +import java.lang.Thread; |
| 39 | + |
| 40 | + |
| 41 | +class AudioThread { |
| 42 | + |
| 43 | + private PythonActivity mParent; |
| 44 | + private AudioTrack mAudio; |
| 45 | + private byte[] mAudioBuffer; |
| 46 | + private int mVirtualBufSize; |
| 47 | + |
| 48 | + public AudioThread(PythonActivity parent) |
| 49 | + { |
| 50 | + mParent = parent; |
| 51 | + mAudio = null; |
| 52 | + mAudioBuffer = null; |
| 53 | + nativeAudioInitJavaCallbacks(); |
| 54 | + } |
| 55 | + |
| 56 | + public int fillBuffer() |
| 57 | + { |
| 58 | + if( mParent.isPaused() ) |
| 59 | + { |
| 60 | + try{ |
| 61 | + Thread.sleep(200); |
| 62 | + } catch (InterruptedException e) {} |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + //if( Globals.AudioBufferConfig == 0 ) // Gives too much spam to logcat, makes things worse |
| 67 | + // mAudio.flush(); |
| 68 | + |
| 69 | + mAudio.write( mAudioBuffer, 0, mVirtualBufSize ); |
| 70 | + } |
| 71 | + |
| 72 | + return 1; |
| 73 | + } |
| 74 | + |
| 75 | + public int initAudio(int rate, int channels, int encoding, int bufSize) |
| 76 | + { |
| 77 | + if( mAudio == null ) |
| 78 | + { |
| 79 | + channels = ( channels == 1 ) ? AudioFormat.CHANNEL_CONFIGURATION_MONO : |
| 80 | + AudioFormat.CHANNEL_CONFIGURATION_STEREO; |
| 81 | + encoding = ( encoding == 1 ) ? AudioFormat.ENCODING_PCM_16BIT : |
| 82 | + AudioFormat.ENCODING_PCM_8BIT; |
| 83 | + |
| 84 | + mVirtualBufSize = bufSize; |
| 85 | + |
| 86 | + if( AudioTrack.getMinBufferSize( rate, channels, encoding ) > bufSize ) |
| 87 | + bufSize = AudioTrack.getMinBufferSize( rate, channels, encoding ); |
| 88 | + |
| 89 | + /** |
| 90 | + if(Globals.AudioBufferConfig != 0) { // application's choice - use minimal buffer |
| 91 | + bufSize = (int)((float)bufSize * (((float)(Globals.AudioBufferConfig - 1) * 2.5f) + 1.0f)); |
| 92 | + mVirtualBufSize = bufSize; |
| 93 | + } |
| 94 | + **/ |
| 95 | + mAudioBuffer = new byte[bufSize]; |
| 96 | + |
| 97 | + mAudio = new AudioTrack(AudioManager.STREAM_MUSIC, |
| 98 | + rate, |
| 99 | + channels, |
| 100 | + encoding, |
| 101 | + bufSize, |
| 102 | + AudioTrack.MODE_STREAM ); |
| 103 | + mAudio.play(); |
| 104 | + } |
| 105 | + return mVirtualBufSize; |
| 106 | + } |
| 107 | + |
| 108 | + public byte[] getBuffer() |
| 109 | + { |
| 110 | + return mAudioBuffer; |
| 111 | + } |
| 112 | + |
| 113 | + public int deinitAudio() |
| 114 | + { |
| 115 | + if( mAudio != null ) |
| 116 | + { |
| 117 | + mAudio.stop(); |
| 118 | + mAudio.release(); |
| 119 | + mAudio = null; |
| 120 | + } |
| 121 | + mAudioBuffer = null; |
| 122 | + return 1; |
| 123 | + } |
| 124 | + |
| 125 | + public int initAudioThread() |
| 126 | + { |
| 127 | + // Make audio thread priority higher so audio thread won't get underrun |
| 128 | + Thread.currentThread().setPriority(Thread.MAX_PRIORITY); |
| 129 | + return 1; |
| 130 | + } |
| 131 | + |
| 132 | + public int pauseAudioPlayback() |
| 133 | + { |
| 134 | + if( mAudio != null ) |
| 135 | + { |
| 136 | + mAudio.pause(); |
| 137 | + return 1; |
| 138 | + } |
| 139 | + return 0; |
| 140 | + } |
| 141 | + |
| 142 | + public int resumeAudioPlayback() |
| 143 | + { |
| 144 | + if( mAudio != null ) |
| 145 | + { |
| 146 | + mAudio.play(); |
| 147 | + return 1; |
| 148 | + } |
| 149 | + return 0; |
| 150 | + } |
| 151 | + |
| 152 | + private native int nativeAudioInitJavaCallbacks(); |
| 153 | +} |
| 154 | + |
0 commit comments