ak1394 / react-native-tts

React Native Text-To-Speech library for Android and iOS
622 stars 158 forks source link

how to make TTS voice louder #75

Open fedeerbes opened 6 years ago

fedeerbes commented 6 years ago

Hi guys, this module is awsome, but i wonder if exist any chance to make the voice louder, like Directions of google maps o the voice of WAZE.

i new on android, i tried to use Equalizer y BassBoost with a audioSessionId from audiomanager and pass this to params on speak method. But without luck

 public void speak(String utterance, ReadableMap params, Promise promise) {
        if(notReady(promise)) return;
        **int audioSessionId = audioManager.generateAudioSessionId();**
        if(ducking) {
            int amResult;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                ...
            } else {
                amResult = audioManager.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

                Log.d(TAG, "REACT NATIVE TTS: audioSessionId audio session ID: " + audioSessionId);
                mEqualizer = new Equalizer(0, **audioSessionId**);
                mBassBoost = new BassBoost(0, **audioSessionId**);
                mBassBoost.setStrength((short) ((short) 1000 / 100 * 90));
                mBassBoost.setEnabled(true);
                short bands = mEqualizer.getNumberOfBands();
                final short minEQLevel = mEqualizer.getBandLevelRange()[0];
                final short maxEQLevel = mEqualizer.getBandLevelRange()[1];
                for (short i = 0; i < bands; i++) {
                    final short band = i;
                    Log.d(TAG, "REACT NATIVE TTS: bandLevel: " + mEqualizer.getBandLevel(band));
                    mEqualizer.setBandLevel(band, (short) maxEQLevel);
                }
                mEqualizer.setEnabled(true);
            }

            if(amResult != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                promise.reject("Android AudioManager error, failed to request audio focus");
                return;
            }
        }

        String utteranceId = Integer.toString(utterance.hashCode());

        int speakResult = speak(utterance, utteranceId, params, **audioSessionId**);
        if(speakResult == TextToSpeech.SUCCESS) {
            promise.resolve(utteranceId);
        } else {
            promise.reject("unable to play");
        }
    }
private int speak(String utterance, String utteranceId, ReadableMap inputParams, int **audioSessionId**) {
        ...
        if (Build.VERSION.SDK_INT >= 21) {
            Bundle params = new Bundle();
            params.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, audioStreamType);
            **params.putInt(TextToSpeech.Engine.KEY_PARAM_SESSION_ID, audioSessionId);**
            params.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, volume);
            params.putFloat(TextToSpeech.Engine.KEY_PARAM_PAN, pan);
            return tts.speak(utterance, TextToSpeech.QUEUE_ADD, params, utteranceId);
        } else {
            HashMap<String, String> params = new HashMap();
            params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
            **params.put(TextToSpeech.Engine.KEY_PARAM_SESSION_ID, String.valueOf(audioSessionId));**
            params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(audioStreamType));
            params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, String.valueOf(volume));
            params.put(TextToSpeech.Engine.KEY_PARAM_PAN, String.valueOf(pan));
            return tts.speak(utterance, TextToSpeech.QUEUE_ADD, params);
        }
    }
omatrot commented 6 years ago

I have the same problem, the voice volume is too low, which makes it difficult to use outside