SoFunction
Updated on 2024-11-12

Example of a Python Audio Generator Implementation

Generating audio with different sounds using Python

The first step is to go to Baidu AI to register an account, create a voice technology application in the console, get the AppID, API Key, Secret Key

Step 2 Citation

from tkinter import *
from  import askdirectory
from aip import AipSpeech
from tkinter import ttk

Step 3 Build Forms

root = Tk()
('Generate speech') 
path = StringVar()
pathmc=StringVar() 
pathnr=StringVar() 

Label(root,text = "Save Path:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "Path Selection", command = selectPath).grid(row = 0, column = 3)  
Label(root,text = "Voice name:").grid(row = 2, column = 0)
Entry(root, textvariable = pathmc).grid(row = 2, column = 1)
Label(root,text = "Voice content:").grid(row = 3, column = 0)
Entry(root, textvariable = pathnr).grid(row = 3, column = 1)
Button(root, text = "Save.", command = Save).grid(row = 4, column = 0)  
#DownLoadFrame
Label(root,text = "Sound Type:").grid(row =1, column = 0)
number = StringVar()
 
numberChosen = (root, width=12, textvariable=number)
 
numberChosen['values'] = ('Female Voice', 'Male Voice', 'Degree of freedom', 'Do Ya Ya') 
 
(column=1, row=1) 
 
(0) 

()

Step 4 Create Methods

# Saved Addresses
def selectPath():
 path_ = askdirectory()
 (path_)
 print(path_)
 Parameters for generating audio 
def Save():
   switch = {'Female Voice': 0,       
     'Male Voice': 1,       
     'Degree of freedom': 3, 
     'Do Ya Ya': 4,  
     }

   lx=((),"0")
   yuying((),(),(),lx)
# Generate Audio
def yuying(url,title,contain,lx):
  APP_ID = 'XXX'#Baidu AI in Get
  API_KEY = 'XXX'
  SECRET_KEY = 'XXX'
  client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
  result = (contain, 'zh', 1, {
  'vol': 5,'per':lx,'spd':2,# per Pronunciator Selection, 0 for Female, 1 for Male, 3 for Emotionally Synthesized-Do Easy, 4 for Emotionally Synthesized-Do Ya Ya, Default is Normal Female No
  })
  if not isinstance(result, dict):
    with open(url+'\\'+title+'.mp3', 'wb') as f:
     (result) 

The combined code is

from tkinter import *
from  import askdirectory
from aip import AipSpeech
from tkinter import ttk

def selectPath():
 path_ = askdirectory()
 (path_)
 print(path_)
def Save():
   switch = {'Female Voice': 0,       
     'Male Voice': 1,       
     'Degree of freedom': 3, 
     'Do Ya Ya': 4,  
     }

   lx=((),"0")
   yuying((),(),(),lx)
def yuying(url,title,contain,lx):
  APP_ID = 'XXX'#Baidu AI in Get
  API_KEY = 'XXX'
  SECRET_KEY = 'XXX'
  client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
  result = (contain, 'zh', 1, {
  'vol': 5,'per':lx,'spd':2,# per Pronunciator Selection, 0 for Female, 1 for Male, 3 for Emotionally Synthesized-Do Easy, 4 for Emotionally Synthesized-Do Ya Ya, Default is Normal Female No
  })
  if not isinstance(result, dict):
    with open(url+'\\'+title+'.mp3', 'wb') as f:
     (result) 

root = Tk()
('Generate speech') 
path = StringVar()
pathmc=StringVar() 
pathnr=StringVar() 

Label(root,text = "Save Path:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "Path Selection", command = selectPath).grid(row = 0, column = 3)  
Label(root,text = "Voice name:").grid(row = 2, column = 0)
Entry(root, textvariable = pathmc).grid(row = 2, column = 1)
Label(root,text = "Voice content:").grid(row = 3, column = 0)
Entry(root, textvariable = pathnr).grid(row = 3, column = 1)
Button(root, text = "Save.", command = Save).grid(row = 4, column = 0)  

 
Label(root,text = "Sound Type:").grid(row =1, column = 0)
number = StringVar()
 
numberChosen = (root, width=12, textvariable=number)
 
numberChosen['values'] = ('Female Voice', 'Male Voice', 'Degree of freedom', 'Do Ya Ya') 
 
(column=1, row=1) 
 
(0) 

()

rendering (visual representation of how things will turn out)

This is the whole content of this article.