import wave
import sys
import os

arg = sys.argv
if(len(arg) >= 2):
  filename = arg[1];
  origAudio = wave.open(filename,'r')
  frameRate = origAudio.getframerate()
  frames = origAudio.getnframes()
  nChannels = origAudio.getnchannels()
  sampWidth = origAudio.getsampwidth()
  duration = frames / frameRate
  
  origAudio.rewind()
  start = 0
  pos = origAudio.tell();
  while start < duration:
    origAudio.setpos(pos)
    end = min(start+59,duration)
    if(duration - (start + 59) < 30):
      end = duration
    
    framesToRead = (end - start) * frameRate;
    frame_data = origAudio.readframes(framesToRead);
    pos = origAudio.tell()
    newFile = filename + '_time=' + str(start/60)
    subchunk = wave.open(newFile,'w')
    subchunk.setnchannels(nChannels)
    subchunk.setsampwidth(sampWidth);
    subchunk.setframerate(frameRate)
    subchunk.writeframes(frame_data)
    subchunk.close()
    start = end + 1
  
  
  os.remove(filename);  


@@ -31,62 +31,468 @@
 os%0A%0A
-arg = sys.argv%0Aif(len(arg) %3E= 2):%0A  filename = arg%5B1%5D;
+%0Adef traverseThroughFolder(current_path):%0A  files_and_folders = os.listdir(current_path);%0A  for file in files_and_folders:  %0A    file_path = current_path + '/' + file;%0A    if(os.path.isdir(file_path)):%0A      traverseThroughFolder(file_path,time_csv,hr_csv_dir)%0A    else:%0A      splitAudio(file_path); #splits audio file into subchunks and deletes the original file%0A  return%0A%0Adef splitAudio(file_path):%0A  int window_length = 59; #duration of subchunks in seconds
 %0A  o
@@ -520,18 +520,28 @@
 file
-name
+_path
 ,'r')%0A
+  try:%0A  
   fr
@@ -575,16 +575,18 @@
 erate()%0A
+  
   frames
@@ -611,16 +611,18 @@
 rames()%0A
+  
   nChann
@@ -652,16 +652,18 @@
 nnels()%0A
+  
   sampWi
@@ -695,16 +695,18 @@
 dth()%0A  
+ 
+ 
 duration
@@ -730,33 +730,15 @@
 Rate
+ 
 %0A  %0A  
-origAudio.rewind()%0A
   st
@@ -745,16 +745,18 @@
 art = 0%0A
+  
   pos = 
@@ -771,18 +771,19 @@
 o.tell()
-;
 %0A
+  
   while 
@@ -804,16 +804,18 @@
 on:%0A    
+ 
+ 
 origAudi
@@ -828,24 +828,26 @@
 os(pos)%0A    
+  
 end = min(st
@@ -850,18 +850,29 @@
 n(start+
-59
+window_length
 ,duratio
@@ -878,16 +878,18 @@
 on)%0A    
+  
 if(durat
@@ -907,16 +907,41 @@
 t + 
-59) %3C 30
+window_length) %3C= window_length/2
 ):%0A 
@@ -941,24 +941,26 @@
 h/2):%0A      
+ 
+ 
 end = durati
@@ -967,16 +967,18 @@
 on%0A    %0A
+  
     fram
@@ -1013,18 +1013,19 @@
 rameRate
-;
 %0A
+  
     fram
@@ -1067,18 +1067,19 @@
 sToRead)
-;
 %0A
+  
     pos 
@@ -1093,24 +1093,26 @@
 udio.tell()%0A
+  
     newFile 
@@ -1148,13 +1148,12 @@
 tart
-/60
 )%0A
+  
     
@@ -1186,16 +1186,31 @@
 le,'w')%0A
+      try:%0A    
     subc
@@ -1238,16 +1238,20 @@
 annels)%0A
+    
     subc
@@ -1278,18 +1278,21 @@
 mpWidth)
-;
 %0A
+    
     subc
@@ -1320,24 +1320,28 @@
 meRate)%0A    
+  
+  
 subchunk.wri
@@ -1361,16 +1361,35 @@
 e_data)%0A
+      finally:%0A    
     subc
@@ -1397,24 +1397,61 @@
 unk.close()%0A
+    finally:%0A      origAudio.close()%0A
     start = 
@@ -1488,10 +1488,137 @@
 name);  
+%0A  %0A%0Aarg = sys.argv%0Aif(len(arg) %3E= 2):%0A  root_dir = arg%5B1%5D;%0A  if(os.path.isdir(root_dir):%0A    traverseThroughFolder(root_dir%0A  
 %0A%0A

Update wav_split.py