Issue on rejoining splitted AudioSegment

66 Views Asked by At

I'm working on a script that splits an AudioSegment into 2 second sub segments and then rejoin them. The final purpose of this is to apply a transform function to each segment before rejoining them but at this point I'm just trying to rejoin the segments and left that transform out of the execution

I'm executing the following code:

song = Auseg.from_file(sys.argv[1])
song = song.set_channels(1)

song = song.set_frame_rate(12000)

fr = song.frame_rate
max = song.max
leng = song.duration_seconds
framecount =  int(song.frame_count())

songsegs = song.dice(2) #split AudioSegment into a list of 2s segments
print(len(songsegs))

print(framecount," Frames")
print("Framerate: ", fr)
print("Preparing...")
output = Auseg.empty()
for n in range(0,len(songsegs)):
    new_segment = songsegs[n] #leave_only_peaks(songsegs[n])
    #print(new_segment)
    output = output + new_segment
    sys.stdout.write("\r%i segments processed" % n)
    sys.stdout.flush()
print("\nExporting as prepared.wav..")
#print(output.tobytes())
output.export("prepared.wav",format="wav")

I was able to confirm that using the export() method on a single segment outputs a 2s part of the song as expected, but when rejoining all segments with the code above (which tecnically should return the original song) I get this weird file that is 1600 hours long. I tried another implementation that concats numpy arrays and then converts the result back to an AudioSegment but got the same result

output file

Thank you very much for reading, I'm open for any suggestion

1

There are 1 best solutions below

0
On

Oddly enought it seems the issue was generated for concating the segments on top of the segment generated by the empty() method, with this tweak I was able to make it work

output = songsegs[0]
for n in range(1,len(songsegs)):
    new_segment = songsegs[n] #leave_only_peaks(songsegs[n])
    #print(new_segment)
    output = output + songsegs[n]
    sys.stdout.write("\r%i segments processed" % n)
    sys.stdout.flush()

Still would love to know your takes on this, thanks!