Re: audio collage?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Obviously i forgot to attach the file ;)

here it is

renato



On Sat, 9 Jan 2010 00:31:42 +0100
Renato <rennabh@xxxxxxxxx> wrote:

> On Mon, 4 Jan 2010 17:05:27 -0600
> Josh Lawrence <hardbop200@xxxxxxxxx> wrote:
> 
> > On Mon, Jan 4, 2010 at 5:00 PM, Renato <rennabh@xxxxxxxxx> wrote:
> > > very interesting idea! I have never used it, but could 'sox' be
> > > helpful? there's also a "Input File Combining" section in the man
> > > page...
> > 
> > ah!  I'll look into that tonight.  thanks for the tip!
> > 
> 
> 
> HA! I did it! It took me 5 days to learn enough python and sox, but I
> did it! It has worked so far for me, but it's very experimental (it's
> my first non trivial python program). I wouldn't be surprised at all
> if it didn't somehow work...
> 
> if you have python installed, you shold be able to run it with
> ./audiocollage.py dir1 dir2 dir3 ...
> or
> python audiocollage.py dir1 dir2 dir3 ...
> 
> where dir1 etc. are the directories containing mp3's. Unfortunately I
> wasn't able to make it go check recursively.... any ideas? BTW it will
> catch only .mp3 files, not .MP3 or other
> it will then ask for an ouput file, you can type
> file.ext
> where file is the name you want for the collage and ext can be mp3,
> ogg,wav...anything sox supports
> 
> oh obviously you need sox installed
> 
> I think that's all, it's been great fun writing it and using it (I
> love how much music goes in 3 minutes), hope it's usefull
> 
> have I said I'm very proud of myself?
> 
> renato
#! /usr/bin/env python

# This little python script searches in the directories passed to it on the command line (NOT recursively) for mp3 files
# and uses sox to extract random chunks of random length and concatenate them together, with crossfading, creating
# a sort of audio collage from your audio files (which remain untouched!). The default output is 'collage.mp3'
# Please bear with the code, it's full of non-elegant solutions - I'm a python newbie
# sox is a great tool with many effects with which it would be possible to create a crazier collage


#max length in seconds of the audio chunks that will make up the collage. (Percieved length will be smaller due
#to crossfades
max = 14 
##WARNING## *all* input files have to be longer than max

import os
import commands
import random
import sys

#let's make a working directory 

tmp = 'audiocollagetemporaryfolder/'
os.system('mkdir -p {0}'.format(tmp))

def duration(x):
	"""Returns duration of an mp3 in seconds, ignoring warnings in the ouput"""
	
	os.system('touch {0}soxiout'.format(tmp))
	os.system('soxi -D "{0}" >> {1}soxiout'.format(x,tmp))
	f = open('{0}soxiout'.format(tmp),'r')
	g = f.readline()
	while g != '':
		dura = g[:-2]
		h = f.readline()
		if h == '':
			break
		else: 
			g = h
	return int(float(g))
	os.system('rm -f {0}soxiout'.format(tmp))
	


#ask for the output file:
print ''
print ''
out = raw_input('output file (it *WILL* overwrite existing files) - default is collage.mp3: ')

if out == '':
	out = 'collage.mp3'

#first thing get the directories in which to search mp3s
#print sys.argv
#print ''
#print len(sys.argv)
directories = []
for i in range(1,len(sys.argv)):
	directories.append(sys.argv[i])

#then put all the files in these directories in a list

files = []
for i in directories:
	tmplist = os.listdir('{0}'.format(i))
	for h in tmplist:
		files.append(i + '/' + h)
	
#next make a new list with only mp3 files

mp3 = []
for i in files:
	if i[-3:] == 'mp3':
		mp3.append(i)

#on to the fun stuff: one by one, trim the audio files and save them to audiocollagetemporaryfolder/[number].wav

i = 0
while mp3 != []:
	a = random.choice(mp3)		#randomly select an mp3, then take away that item from the mp3 list
	c = mp3[0:mp3.index(a)]		
	c.extend(mp3[mp3.index(a) + 1:])
	mp3 = c
	dura = duration(a);	
	td = [dura, random.randint(0,dura - max), random.randint(7,max)] #td stands for trim data - storing in it info needed for sox

	#resampling is needed because the splice effect (used afterwards) wants files with same sample rate
	cmd = 'sox  "{0}" {1}{2}.wav rate 44.1k trim {3} {4}'.format(a,tmp,i,td[1],td[2]) 

	print 'trimming ', a
	print ' '  
	os.system(cmd)
	i = i + 1

#now using sox's splice effect we'll concatenate with crossfading first 0.wav and 1.wav, writing the result
#on 1.wav, then 1.wav and 2.wav writing on 2.wav... and so on

print '-'*20
print 'attaching all the chunks together'

j = 0
while j < i - 1:
	arg = '{0}{1}.wav'.format(tmp,j)
	dur = duration(arg)
	cmd = 'sox {0}{1}.wav {0}{2}.wav {0}a.wav splice -q {3},{4}'.format(tmp,j,j+1,dur,random.randint(1,2))
	print cmd
	os.system(cmd)
	os.system('rm -f {0}{1}.wav'.format(tmp,j+1))
	os.system('mv {0}a.wav {0}{1}.wav'.format(tmp, j+1))
	j = j + 1


#final conversion to output file, adding fade in, out and removing working directory

print '-'*20
print 'exporting to the selected output file/type'

cmd = 'sox {0}{1}.wav {2} fade t 2 0'.format(tmp,j - 1,out)
print cmd
os.system(cmd)


print 'removing temporary files'
os.system('rm -rf {0}'.format(tmp))

print 'Done'


_______________________________________________
Linux-audio-user mailing list
Linux-audio-user@xxxxxxxxxxxxxxxxxxxx
http://lists.linuxaudio.org/listinfo/linux-audio-user

[Index of Archives]     [Linux Sound]     [ALSA Users]     [Pulse Audio]     [ALSA Devel]     [Sox Users]     [Linux Media]     [Kernel]     [Photo Sharing]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux