Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | package android.speech.tts; |
| 17 | |
| 18 | import android.media.AudioFormat; |
Narayan Kamath | 563fd3a | 2011-10-27 13:41:54 +0100 | [diff] [blame] | 19 | import android.os.FileUtils; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 20 | import android.util.Log; |
| 21 | |
| 22 | import java.io.File; |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 23 | import java.io.FileOutputStream; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | import java.io.RandomAccessFile; |
| 26 | import java.nio.ByteBuffer; |
| 27 | import java.nio.ByteOrder; |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 28 | import java.nio.channels.FileChannel; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Speech synthesis request that writes the audio to a WAV file. |
| 32 | */ |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 33 | class FileSynthesisCallback extends AbstractSynthesisCallback { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 34 | |
| 35 | private static final String TAG = "FileSynthesisRequest"; |
| 36 | private static final boolean DBG = false; |
| 37 | |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 38 | private static final int MAX_AUDIO_BUFFER_SIZE = 8192; |
| 39 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 40 | private static final int WAV_HEADER_LENGTH = 44; |
| 41 | private static final short WAV_FORMAT_PCM = 0x0001; |
| 42 | |
| 43 | private final Object mStateLock = new Object(); |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 44 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 45 | private int mSampleRateInHz; |
| 46 | private int mAudioFormat; |
| 47 | private int mChannelCount; |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 48 | |
| 49 | private FileChannel mFileChannel; |
| 50 | |
| 51 | private boolean mStarted = false; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 52 | private boolean mStopped = false; |
Bjorn Bringert | 360eb16 | 2011-04-19 09:20:35 +0100 | [diff] [blame] | 53 | private boolean mDone = false; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 54 | |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 55 | FileSynthesisCallback(FileChannel fileChannel) { |
| 56 | mFileChannel = fileChannel; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | void stop() { |
| 61 | synchronized (mStateLock) { |
| 62 | mStopped = true; |
| 63 | cleanUp(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Must be called while holding the monitor on {@link #mStateLock}. |
| 69 | */ |
| 70 | private void cleanUp() { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 71 | closeFile(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Must be called while holding the monitor on {@link #mStateLock}. |
| 76 | */ |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 77 | private void closeFile() { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 78 | try { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 79 | if (mFileChannel != null) { |
| 80 | mFileChannel.close(); |
| 81 | mFileChannel = null; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 82 | } |
| 83 | } catch (IOException ex) { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 84 | Log.e(TAG, "Failed to close output file descriptor", ex); |
Narayan Kamath | 563fd3a | 2011-10-27 13:41:54 +0100 | [diff] [blame] | 85 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 89 | public int getMaxBufferSize() { |
| 90 | return MAX_AUDIO_BUFFER_SIZE; |
| 91 | } |
| 92 | |
| 93 | @Override |
Bjorn Bringert | 360eb16 | 2011-04-19 09:20:35 +0100 | [diff] [blame] | 94 | boolean isDone() { |
| 95 | return mDone; |
| 96 | } |
| 97 | |
| 98 | @Override |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 99 | public int start(int sampleRateInHz, int audioFormat, int channelCount) { |
| 100 | if (DBG) { |
| 101 | Log.d(TAG, "FileSynthesisRequest.start(" + sampleRateInHz + "," + audioFormat |
| 102 | + "," + channelCount + ")"); |
| 103 | } |
| 104 | synchronized (mStateLock) { |
| 105 | if (mStopped) { |
| 106 | if (DBG) Log.d(TAG, "Request has been aborted."); |
| 107 | return TextToSpeech.ERROR; |
| 108 | } |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 109 | if (mStarted) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 110 | cleanUp(); |
| 111 | throw new IllegalArgumentException("FileSynthesisRequest.start() called twice"); |
| 112 | } |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 113 | mStarted = true; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 114 | mSampleRateInHz = sampleRateInHz; |
| 115 | mAudioFormat = audioFormat; |
| 116 | mChannelCount = channelCount; |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 117 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 118 | try { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 119 | mFileChannel.write(ByteBuffer.allocate(WAV_HEADER_LENGTH)); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 120 | return TextToSpeech.SUCCESS; |
| 121 | } catch (IOException ex) { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 122 | Log.e(TAG, "Failed to write wav header to output file descriptor" + ex); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 123 | cleanUp(); |
| 124 | return TextToSpeech.ERROR; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int audioAvailable(byte[] buffer, int offset, int length) { |
| 131 | if (DBG) { |
| 132 | Log.d(TAG, "FileSynthesisRequest.audioAvailable(" + buffer + "," + offset |
| 133 | + "," + length + ")"); |
| 134 | } |
| 135 | synchronized (mStateLock) { |
| 136 | if (mStopped) { |
| 137 | if (DBG) Log.d(TAG, "Request has been aborted."); |
| 138 | return TextToSpeech.ERROR; |
| 139 | } |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 140 | if (mFileChannel == null) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 141 | Log.e(TAG, "File not open"); |
| 142 | return TextToSpeech.ERROR; |
| 143 | } |
| 144 | try { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 145 | mFileChannel.write(ByteBuffer.wrap(buffer, offset, length)); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 146 | return TextToSpeech.SUCCESS; |
| 147 | } catch (IOException ex) { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 148 | Log.e(TAG, "Failed to write to output file descriptor", ex); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 149 | cleanUp(); |
| 150 | return TextToSpeech.ERROR; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public int done() { |
| 157 | if (DBG) Log.d(TAG, "FileSynthesisRequest.done()"); |
| 158 | synchronized (mStateLock) { |
Narayan Kamath | 9c3d7a8 | 2012-07-20 18:01:43 +0100 | [diff] [blame] | 159 | if (mDone) { |
| 160 | if (DBG) Log.d(TAG, "Duplicate call to done()"); |
| 161 | // This preserves existing behaviour. Earlier, if done was called twice |
| 162 | // we'd return ERROR because mFile == null and we'd add to logspam. |
| 163 | return TextToSpeech.ERROR; |
| 164 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 165 | if (mStopped) { |
| 166 | if (DBG) Log.d(TAG, "Request has been aborted."); |
| 167 | return TextToSpeech.ERROR; |
| 168 | } |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 169 | if (mFileChannel == null) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 170 | Log.e(TAG, "File not open"); |
| 171 | return TextToSpeech.ERROR; |
| 172 | } |
| 173 | try { |
| 174 | // Write WAV header at start of file |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 175 | mFileChannel.position(0); |
| 176 | int dataLength = (int) (mFileChannel.size() - WAV_HEADER_LENGTH); |
| 177 | mFileChannel.write( |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 178 | makeWavHeader(mSampleRateInHz, mAudioFormat, mChannelCount, dataLength)); |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 179 | closeFile(); |
Bjorn Bringert | 360eb16 | 2011-04-19 09:20:35 +0100 | [diff] [blame] | 180 | mDone = true; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 181 | return TextToSpeech.SUCCESS; |
| 182 | } catch (IOException ex) { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 183 | Log.e(TAG, "Failed to write to output file descriptor", ex); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 184 | cleanUp(); |
| 185 | return TextToSpeech.ERROR; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 190 | @Override |
Bjorn Bringert | 360eb16 | 2011-04-19 09:20:35 +0100 | [diff] [blame] | 191 | public void error() { |
| 192 | if (DBG) Log.d(TAG, "FileSynthesisRequest.error()"); |
| 193 | synchronized (mStateLock) { |
| 194 | cleanUp(); |
| 195 | } |
| 196 | } |
| 197 | |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 198 | private ByteBuffer makeWavHeader(int sampleRateInHz, int audioFormat, int channelCount, |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 199 | int dataLength) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 200 | // TODO: is AudioFormat.ENCODING_DEFAULT always the same as ENCODING_PCM_16BIT? |
| 201 | int sampleSizeInBytes = (audioFormat == AudioFormat.ENCODING_PCM_8BIT ? 1 : 2); |
| 202 | int byteRate = sampleRateInHz * sampleSizeInBytes * channelCount; |
| 203 | short blockAlign = (short) (sampleSizeInBytes * channelCount); |
| 204 | short bitsPerSample = (short) (sampleSizeInBytes * 8); |
| 205 | |
| 206 | byte[] headerBuf = new byte[WAV_HEADER_LENGTH]; |
| 207 | ByteBuffer header = ByteBuffer.wrap(headerBuf); |
| 208 | header.order(ByteOrder.LITTLE_ENDIAN); |
| 209 | |
| 210 | header.put(new byte[]{ 'R', 'I', 'F', 'F' }); |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 211 | header.putInt(dataLength + WAV_HEADER_LENGTH - 8); // RIFF chunk size |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 212 | header.put(new byte[]{ 'W', 'A', 'V', 'E' }); |
| 213 | header.put(new byte[]{ 'f', 'm', 't', ' ' }); |
| 214 | header.putInt(16); // size of fmt chunk |
| 215 | header.putShort(WAV_FORMAT_PCM); |
| 216 | header.putShort((short) channelCount); |
| 217 | header.putInt(sampleRateInHz); |
| 218 | header.putInt(byteRate); |
| 219 | header.putShort(blockAlign); |
| 220 | header.putShort(bitsPerSample); |
| 221 | header.put(new byte[]{ 'd', 'a', 't', 'a' }); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 222 | header.putInt(dataLength); |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 223 | header.flip(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 224 | |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 225 | return header; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | } |