How to sum every row of a permutation in python

201 views Asked by At

My goal is to differently sum ten audio files obtained from the permutations in order to have a different audio file for each permutation (10! = 3 628 800 final audio in total) and export them as wav files.

Example (sound1 + sound3 + sound2+ sound4 + sound 8 + sound6 + sound5 + sound7 + sound10 + sound9 = first audio etc.)

How can I do this?. I tried but I'm having some trouble with the sum.

Here's what I've done so far

from pydub import AudioSegment
import itertools

sound1 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/1_R.wav")
sound2 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/2_R.wav")
sound3 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/3_R.wav")
sound4 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/4_R.wav")
sound5 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/5_R.wav")
sound6 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/6_R.wav")
sound7 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/7_R.wav")
sound8 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/8_R.wav")
sound9 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/9_R.wav")
sound10 = AudioSegment.from_wav("/Users/Desktop/Countingpuppet/Music/10_R.wav")

lis = (sound1,sound2,sound3,sound4,sound5,sound6,sound7,sound8,sound9,sound10)

list(itertools.permutations(lis))

dc = []
for i in range(1, len(lis) + 1):
    #sum(list(itertools.permutations(lis, i)))
    sum(lis(i))
    #dc += lis(i)

#combined_sounds = sound1 + sound2
#combined_sounds.export("/output/path.wav", format="wav")

1

There are 1 answers

1
jimifiki On

Let myPerm be a permutation of the integers {0,...,9} you probably want this:

sum([lis(i) for i in myPerm])

but, if sum is commuative (i.e. a+b == b+a), you probably don't really want what you claim you want to achieve.

EDIT:

After I read your edit I think that you want to do the analogous of this

t = ("ba","a","c")
["".join(i) for i in itertools.permutations(t)]

for audio files.

By giving a look to this answer: https://stackoverflow.com/a/30204581/512225 I deduce that you have to replace "".join(i) with sum(i, AudioSegment.empty()). I hope this solves your problem. (I didn't try it nyself on my machine)