from aip import AipSpeech #Create a client object client = AipSpeech(appId, apiKey, secretKey) # Set up the conversion options options = { 'spd': 4, # Speech speed 'vol': 5, # Volume 'per': 1 # Voice type - 0: female, 1: male, 3: neutral } # Text to be converted text = 'Welcome to the Python world' # Convert text to speech result = client.synthesis(text, 'zh', 1, options) # Save the speech to a file if not isinstance(result, dict): with open('output.mp3', 'wb') as f: f.write(result)
from aip import AipSpeech #Create a client object client = AipSpeech(appId, apiKey, secretKey) # Set up the recognition options options = { 'dev_pid': 1536, # Recognition model - 1536: general recognition } # Audio file to be recognized with open('audio.wav', 'rb') as f: audio_data = f.read() # Recognize the speech in the audio file result = client.asr(audio_data, 'wav', 16000, options) # Display the recognized speech if result['err_no'] == 0: print(result['result'][0]) else: print('Recognition error')In both examples, we import the AipSpeech library and create a client object by providing our Baidu API credentials. We then use different APIs provided by the library for text to speech conversion and speech recognition. The library used in these examples is `aip`.