반응형

마이크를 통해 입력된 음성 데이터를 스피커로 출력해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import sounddevice as sd
import numpy as np
 
def callback(indata, outdata, frames, time, status):
    volume_norm = np.linalg.norm(indata)    
    print("Volume: " + '='*(int(volume_norm)) + ' '*(79-(int(volume_norm))) + '\r', end = '')
    
    # indata를 outdata에 넣으면 마이크로 넘어온 데이터가 스피커로 출력된다.
    outdata[:] = indata
 
try:
    with sd.Stream(callback=callback):
        input("Press Enter to quit.\n\n")
except KeyboardInterrupt:
    print("exit.")
 

 

 

코드를 실행하면 마이크에 입력된 음성이 스피커로 출력된다.

 

python-sounddevice

PyAudio Examples

 

반응형
Posted by J-sean
:
반응형

파이썬에서 Google Speech to Text를 사용해 보자.

 

SpeechRecognition을 설치한다.

 

마이크 사용을 위해 PyAudio를 설치한다.

 

소스를 입력하고 실행한다.

 

완벽하지는 않지만 꽤 잘 인식한다. ('현'이 아니라 '션'이었다)

 

 

마이크가 아닌 음성 파일을 이용하는 경우 위와 같이 소스를 수정한다.

 

get_XXX_data()를 이용하면 오디오 데이터를 raw, wav, flac 등의 파일로 저장할 수 있다.

 

※ 참고

SpeechRecognition

 

반응형
Posted by J-sean
:

Text To Speech - gTTS

Python 2023. 4. 30. 22:26 |
반응형

파이썬에서 Google Text to Speech를 사용해 보자.

 

gTTS를 설치한다.

 

playsound를 설치한다.

playsound 1.3.0이 정상 작동 하지 않으면 1.2.2를 설치한다.

pip install playsound==1.2.2

 

소스를 입력하고 실행한다.

 

tts.mp3
0.01MB

 

※ 참고

gTTS Documentation

 

반응형
Posted by J-sean
:
반응형

텍스트를 음성으로 합성해 주는 TTS(Text To Speech)기능을 사용해 보자.

 

레이아웃에 에디트텍스트와 버튼을 적당히 배치한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.myapplication;
 
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
import androidx.appcompat.app.AppCompatActivity;
 
import java.util.Locale;
 
public class MainActivity extends AppCompatActivity {
 
    TextToSpeech tts;
    EditText editText;
    Button button;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tts = new TextToSpeech(thisnew TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = tts.setLanguage(Locale.ENGLISH);
                    // 영어로 설정해도 한글을 읽을 수 있고 영어 발음이 한국어로 설정하는것 보다 낫다.
                    if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA) {
                        Log.e("TTS""Language not supported.");
                    } else {
                        button.setText("Ready To Speak");
                    }
                } else {
                    Log.e("TTS""Initialization failed.");
                }
            }
        });
        editText = findViewById(R.id.editText);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CharSequence text = editText.getText();
                tts.setPitch((float)1.0); // Sets the speech pitch for the TextToSpeech engine.
                tts.setSpeechRate((float)1.0); // Sets the speech rate.
 
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, null"uid");
                // QUEUE_ADD - Queue mode where the new entry is added at the end of the playback queue.
                // QUEUE_FLUSH - Queue mode where all entries in the playback queue (media to be played
                // and text to be synthesized) are dropped and replaced by the new entry.
            }
        });
    }
 
    @Override
    protected void onDestroy() {
        if (tts != null) {
            tts.stop();
            // Interrupts the current utterance (whether played or rendered to file) and
            // discards other utterances in the queue.
            tts.shutdown();
            // Releases the resources used by the TextToSpeech engine.
        }
        super.onDestroy();
    }
}
 

 

소스를 입력하고 빌드한다.

 

텍스트를 입력하고 버튼을 터치하면 음성이 출력된다.

※ 참고

TextToSpeech

 

반응형
Posted by J-sean
: