@@ -0,0 +1,2056 @@
+from scipy.io.wavfile import write%0Aimport numpy as np%0Afrom numpy import linspace,sin,pi,int16%0Afrom pylab import plot,show,axis%0Aimport scipy%0Aimport math%0A%0Afrom pax.datastructure import Event%0Afrom pax import plugin%0A%0Aclass WavOutput(plugin.OutputPlugin):%0A    def note(freq, len, amp=1, rate=44100):%0A        # tone synthesis  %0A        t = linspace(0,len,len*rate)%0A        data = sin(2*pi*freq*t)*amp%0A        return data.astype(int16) # two byte integers%0A%0A    def startup(self):%0A        self.all_data = %7B%7D%0A%0A        self.start_time = None%0A        %0A%0A    def write_event(self, event):%0A        self.log.debug('Writing event')%0A%0A%0A        data = event.pmt_waveforms.sum(0)%0A%0A        self.all_data%5Bevent.start_time//event.sample_duration%5D = data.copy()%0A%0A        # A tone, 2 seconds, 44100 samples per second                                                                                                                                       %0A        #tone = note(440,2,amp=10000)%0A%0A        #write('440hzAtone.wav',44100,tone) # writing the sound to a file                                                                                                                    %0A%0A        #plot(linspace(0,2,2*44100),tone)%0A        #axis(%5B0,0.4,15000,-15000%5D)%0A        #show()%0A%0A        write('song_%25d.wav' %25 event.event_number,%0A              44100,%0A              data)%0A%0A    def shutdown(self):%0A        start = min(self.all_data.keys())%0A        end = max(self.all_data.keys())%0A%0A        print(start, end)%0A%0A        data = np.zeros(end - start + 40000, dtype=np.int16)%0A        for key, value in self.all_data.items():%0A            key -= start%0A            key = int(key)%0A            data%5Bkey : key + len(value)%5D += value%0A            print(key, value)%0A%0A        write('all.wav', 10**8, data)%0A        %0A        R = 10**8 // 44100%0A        pad_size = math.ceil(float(data.size)/R)*R - data.size%0A        b_padded = np.append(data, np.zeros(pad_size)*np.NaN)%0A        new_data = scipy.nanmean(b_padded.reshape(-1,R), axis=1)%0A        %0A        write('all2.wav', 44100, new_data)%0A        %0A        %0A

Output waveforms to music