def test_WriteFrames(self): _sample_new = "newFile.wav" # save first signals.save( _sample_new, self._sample_1 ) # read the saved file and compare Audio() instances newFile = signals.open( _sample_new ) self.assertEqual(newFile.get_framerate(), self._sample_1.get_framerate()) self.assertEqual(newFile.get_sampwidth(), self._sample_1.get_sampwidth()) self.assertEqual(newFile.get_nchannels(), self._sample_1.get_nchannels()) self.assertEqual(newFile.get_nframes(), self._sample_1.get_nframes()) newFile.close() os.remove(_sample_new) self._sample_1.rewind() signals.save_fragment( _sample_new, self._sample_1, self._sample_1.read_frames(self._sample_1.get_nframes())) newFile = signals.open( _sample_new ) self.assertEqual(newFile.get_framerate(), self._sample_1.get_framerate()) self.assertEqual(newFile.get_sampwidth(), self._sample_1.get_sampwidth()) self.assertEqual(newFile.get_nchannels(), self._sample_1.get_nchannels()) self.assertEqual(newFile.get_nframes(), self._sample_1.get_nframes()) newFile.close() os.remove(_sample_new) _sample_new = "newFile.aiff" # save first signals.save( _sample_new, self._sample_4 ) # read the saved file and compare Audio() instances newFile = signals.open( _sample_new ) self.assertEqual(newFile.get_framerate(), self._sample_4.get_framerate()) self.assertEqual(newFile.get_sampwidth(), self._sample_4.get_sampwidth()) self.assertEqual(newFile.get_nchannels(), self._sample_4.get_nchannels()) self.assertEqual(newFile.get_nframes(), self._sample_4.get_nframes()) newFile.close() os.remove(_sample_new) self._sample_4.rewind() signals.save_fragment( _sample_new, self._sample_4, self._sample_4.read_frames(self._sample_4.get_nframes())) newFile = signals.open( _sample_new ) self.assertEqual(newFile.get_framerate(), self._sample_4.get_framerate()) self.assertEqual(newFile.get_sampwidth(), self._sample_4.get_sampwidth()) self.assertEqual(newFile.get_nchannels(), self._sample_4.get_nchannels()) self.assertEqual(newFile.get_nframes(), self._sample_4.get_nframes()) newFile.close() os.remove(_sample_new)
def write_tracks(self, trstracks, output, ext="txt", trsunits=[], trsnames=[], logfile=None): """ Write tracks in an output directory. Print only errors in a log file. @param trstracks All tracks as an array of tuples (start,end) @param output Directory name (String) @param ext Tracks file names extension (String) @param trsunits Tracks content (Array of string) @param trsnames Expected tracks file's names (Array of string) @param logfile Log file (sppasLog) """ wavtraks = True if not os.path.exists( output ): os.mkdir( output ) # Write text tracks for i in range(len(trsunits)): trackbasename = "" if len(trsnames) > 0: # Specific names are given trackbasename = os.path.join(output, trsnames[i]) else: trackbasename = os.path.join(output, "track_%.06d" % (i+1)) if len(trackbasename)>0: # Write the transcription track content (if any) if len(trsunits)>0: tracktxtname = trackbasename+"."+ext if isinstance(trsunits[0], Transcription): annotationdata.io.write(tracktxtname, trsunits[i]) else: encoding='utf-8' with codecs.open(tracktxtname,"w", encoding) as fp: fp.write(trsunits[i]) elif logfile is not None: logfile.print_message( "Writing track "+tracktxtname,indent=3,status=-1 ) else: print ( " ... ... ... Writing track "+tracktxtname+": [ERROR]" ) # Write wav tracks if self.channelsil.channel is None: return False try: split_tracks = self.channelsil.track_data( trstracks ) except Exception: logfile.print_message('split tracks failed: wav corrupted',indent=2,status=-1) return False for i, split_track in enumerate(split_tracks): trackbasename = "" if len(trsnames) > 0: # Specific names are given trackbasename = os.path.join(output, trsnames[i]) else: trackbasename = os.path.join(output, "track_%.06d" % (i+1)) if len(trackbasename)>0: # Write the wav track content trackwavname = trackbasename+".wav" audio_out = Audio() audio_out.append_channel(self.channelsil.channel) try: signals.save_fragment(trackwavname, audio_out, split_track) except Exception as e: if logfile: logfile.print_message('Writing track %s failed with error: %s'%(trackwavname,str(e)), status=-1) else: print 'Writing track %s failed with error: %s'%(trackwavname,str(e)) wavtraks = False return wavtraks