Skip to content

Commit

Permalink
Modifications with handling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rupin committed Oct 5, 2019
1 parent aaa7286 commit 47d4af3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
Binary file modified Audio Sequence.xlsx
Binary file not shown.
Binary file added Row_2_12.0.wav
Binary file not shown.
Binary file added Row_2_15.0.wav
Binary file not shown.
Binary file added Row_3_23.0.wav
Binary file not shown.
80 changes: 55 additions & 25 deletions createAudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pydub import AudioSegment
import io
from pydub.playback import play
import sys, getopt
import sys, getopt, os


def convertTTS(engtext):
Expand Down Expand Up @@ -41,9 +41,11 @@ def convertTTS(engtext):

return response.audio_content

def createFileName(row, timeslot):
return "Row_"+str(row)+ "_"+str(timeslot)+".wav"

def createAudioFile(row, timeslot, sentence):
filename="Row_"+str(row)+ "_"+str(timeslot)+".wav"
filename=createFileName(row, timeslot)
audioStream=convertTTS(sentence)
#The response's audio_content is binary.
with open(filename, 'wb') as out:
Expand Down Expand Up @@ -82,7 +84,7 @@ def generateAudio(myfile,pending_row=-1):
sys.exit(2)
else:

for row in range (1, 2):
for row in range (1, 5):
timeslot=sheet.cell_value(row, 0)
sentence=sheet.cell_value(row, 1)
createAudioFile(row,timeslot, sentence)
Expand All @@ -96,27 +98,55 @@ def generateAudio(myfile,pending_row=-1):

def combineFiles(inputxlsfilename, outputFileName):

# Advanced usage, if you have raw audio data:
current_audio = AudioSegment(data=audioStream,
sample_width=2,
frame_rate=24000,
channels=1)
#play(sound)
currentDuration=current_audio.duration_seconds

#The duration of the empty slot is equal to time difference between
#the current start time, and the time the audio the sentence took.

emptyduration=timeslot-(lastTiming+lastDuration)
emptyduration=round(emptyduration,3)
blankWAV=AudioSegment.silent(duration=emptyduration*1000,
frame_rate=24000,
)
#play(blankWAV)
combined_sounds=combined_sounds+blankWAV+current_audio
lastTiming=timeslot
lastDuration=currentDuration # dummy, but this has to be initialised by the duration of the current stream
#play(combined_sounds)
wb = xlrd.open_workbook(inputxlsfilename)
sheet = wb.sheet_by_index(0)
lastTiming=0
lastDuration=0
#sheet.cell_value(0, 0)
combined_sounds = AudioSegment.silent(duration=1)
rowcount=sheet.nrows

for row in range (1, 5):
timeslot=sheet.cell_value(row, 0)
sentence=sheet.cell_value(row, 1)
filename=createFileName(row, timeslot)
fileExists=os.path.exists(filename)
if(not fileExists):
createAudioFile(row,timeslot, sentence)

current_audio=AudioSegment.from_wav(filename)

# # Advanced usage, if you have raw audio data:
# current_audio = AudioSegment(data=audioStream,
# sample_width=2,
# frame_rate=24000,
# channels=1)
#play(sound)
currentDuration=current_audio.duration_seconds

#The duration of the empty slot is equal to time difference between
#the current start time, and the time the audio the sentence took.

emptyduration=timeslot-(lastTiming+lastDuration)
emptyduration=round(emptyduration,3)
if(emptyduration<0):
print('Audio in row '+str(row-1)+' exceeds time beyond the start time of row '+str(row))

userChoice=input('Press C/c to continue, X/x to stop processing: ')
if(userChoice.lower()=='c'):
pass
else:
print('Processing Halted')
sys.exit(2)
blankWAV=AudioSegment.silent(duration=emptyduration*1000,frame_rate=24000)

combined_sounds=combined_sounds+blankWAV+current_audio
lastTiming=timeslot
lastDuration=currentDuration # dummy, but this has to be initialised by the duration of the current stream


combined_sounds.export(outputFileName, format="wav")
print(outputFileName+' Saved')


#readXLS('Audio Sequence.xlsx')
Expand Down Expand Up @@ -172,7 +202,7 @@ def main(argv):
generateAudio(inputfile)

elif(operationType=="COMBINE_AUDIO"):
pass
combineFiles(inputfile,outputfile)
elif(operationType=='GENERATE_AUDIO_FOR_ROW'):
generateAudio(inputfile,rownum)
#pass
Expand Down
Binary file added output.wav
Binary file not shown.

0 comments on commit 47d4af3

Please sign in to comment.