]> git.parisson.com Git - telemeta.git/commitdiff
Moved audio generator to ExporterCore.core_process
authoryomguy <>
Sun, 20 May 2007 23:20:38 +0000 (23:20 +0000)
committeryomguy <>
Sun, 20 May 2007 23:20:38 +0000 (23:20 +0000)
telemeta/export/core.py
telemeta/export/flac.py
telemeta/export/mp3.py
telemeta/export/ogg.py
telemeta/export/wav.py
tests/export_test.py

index fd9c1bdbff3f7698a0cada8323162463ff9c2aa1..39bd8f5edcc22a3e5c78062a701a10c200f871df 100644 (file)
@@ -149,17 +149,31 @@ class ExporterCore(Component):
         self.dest = os.path.join(self.dest,target_file)
         return self.dest
 
-    def stream(self,command):
+    def core_process(self,command,buffer_size,dest):
         """Streams encoded audio data through a generator"""
+        
+        __chunk = 0
+        file_out = open(dest,'w')
+
         proc = subprocess.Popen(command,
-                shell=True,
-                bufsize=self.buffer_size,
-                stdin=subprocess.PIPE,
-                stdout=subprocess.PIPE,
-                close_fds=True)
-                
-        chunk = proc.stdout.read(self.buffer_size)
-        return chunk
+                shell = True,
+                bufsize = buffer_size,
+                stdin = subprocess.PIPE,
+                stdout = subprocess.PIPE,
+                close_fds = True)
+
+        __chunk = proc.stdout.read(buffer_size)
+        yield __chunk
+        file_out.write(__chunk)
+
+        # Processing
+        while __chunk:
+            __chunk = proc.stdout.read(buffer_size)
+            yield __chunk
+            file_out.write(__chunk)
+
+        #file_in.close()
+        file_out.close()
 
     def post_process(self, item_id, source, metadata, ext, 
                      cache_dir, options=None):
index 212b61e441d9009bc37f7e31c63bb0db1fcd68ba..6c1d9fc42bc003b70e644235bf271d74ffc3186e 100644 (file)
@@ -85,80 +85,64 @@ class FlacExporter(ExporterCore):
                 media[tag] = str(self.metadata[tag])
         media.save()
         
-    def process(self, item_id, source, metadata, options=None):
-        self.item_id = item_id
-        self.source = source
-        self.metadata = metadata
-        self.options = {}
+    def get_args(self,options=None):
+        """Get process options and return arguments for the encoder"""
         args = ''
-        
         if not options is None:
             self.options = options
-            
-            if 'verbose' in self.options and self.options['verbose'] != '0':
-                args = args
-            else:
+            if not ('verbose' in self.options and self.options['verbose'] != '0'):
                 args = args + ' -s '
-                
+
             if 'flac_quality' in self.options:
                 args = args+' -f -'+self.options['flac_quality']
             else:
                 args = args+' -f -'+self.quality_default
         else:
             args = args+' -s -f -'+self.quality_default
-    
+        return args
+        
+    def process(self, item_id, source, metadata, options=None):
+        self.item_id = item_id
+        self.source = source
+        self.metadata = metadata
+        #self.options = {}
+        self.args = self.get_args(options)
+        self.ext = self.get_file_extension()
+        self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \
+                       '| flac '+self.args+' -c -'
+
+        # Pre-proccessing
         try:
-            # Pre-proccessing (core)
-            self.ext = self.get_file_extension()
             self.dest = self.pre_process(self.item_id,
                                          self.source,
                                          self.metadata,
                                          self.ext,
                                          self.cache_dir,
                                          self.options)
-                                         
-            # Initializing
-            chunk = 0
-            file_out = open(self.dest,'w')
-            
-            proc = subprocess.Popen( \
-                    'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+
-                    '| flac '+args+' -c -',
-                    shell=True,
-                    bufsize=self.buffer_size,
-                    stdin=subprocess.PIPE,
-                    stdout=subprocess.PIPE,
-                    close_fds=True)
-
-            chunk = proc.stdout.read(self.buffer_size)
-            yield chunk
-            file_out.write(chunk)
-
-            # Processing
-            while chunk:
-                chunk = proc.stdout.read(self.buffer_size)
-                yield chunk
-                file_out.write(chunk)           
-            
-            #file_in.close()
-            file_out.close()
+        except:
+            yield 'ExporterError [3]: pre_process'
 
-            # Encoding
-            #os.system('flac '+args+' -o "'+self.dest+'" "'+ \
-            #          self.source+'" > /dev/null')
+        # Processing (streaming + cache writing)
+        try:
+            stream = self.core_process(self.command,self.buffer_size,self.dest)
+            for chunk in stream:
+                yield chunk
+        except:
+            yield 'ExporterError: core_process'
 
-            # Post-proccessing (self)
-            self.write_tags()
+        # Post-proccessing
+        try:
+            self.write_tags()        
             self.post_process(self.item_id,
                          self.source,
                          self.metadata,
                          self.ext,
                          self.cache_dir,
                          self.options)
+        except:
+            yield 'ExporterError: post_process'
 
-            # Output
-            #return self.dest
-
-        except IOError:
-            yield 'ExporterError [3]: source file does not exist.'
 
+        # Encoding
+            #os.system('flac '+args+' -o "'+self.dest+'" "'+ \
+            #          self.source+'" > /dev/null')
index 42155f387decb48a4111fcc9caf0aeb1b876aa8f..a764e92e0a16c122b286651a480add7c94ff451e 100644 (file)
@@ -90,13 +90,9 @@ class Mp3Exporter(ExporterCore):
                 id3.add(frame)
         id3.save()
 
-    def process(self, item_id, source, metadata, options=None):
-        self.item_id = item_id
-        self.source = source
-        self.metadata = metadata
-        self.options = {}
+    def get_args(self,options=None):
+        """Get process options and return arguments for the encoder"""
         args = ''
-        
         if not options is None: 
             self.options = options
             
@@ -113,61 +109,51 @@ class Mp3Exporter(ExporterCore):
             args = args + ' -c -o '
         else:
             args = args + ' -S -c -o '
-        
-        if os.path.exists(self.source) and not iswav16(self.source):
-            self.source = self.decode()
+
+        return args
+
+    def process(self, item_id, source, metadata, options=None):
+        self.item_id = item_id
+        self.source = source
+        self.metadata = metadata
+        #self.options = {}
+        self.args = self.get_args(options)
+        self.ext = self.get_file_extension()
+        self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \
+                       '| lame '+self.args+' --tc "default" - -'
             
+        # Pre-proccessing
         try:
-            # Pre-proccessing (core)
-            self.ext = self.get_file_extension()
             self.dest = self.pre_process(self.item_id,
-                            self.source,
-                            self.metadata,
-                            self.ext,
-                            self.cache_dir,
-                            self.options)
-            
-            # Initializing
-            chunk = 0
-            file_out = open(self.dest,'w')
-            
-            proc = subprocess.Popen( \
-                    'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+
-                    '| lame '+args+' --tc "default" - -',
-                    shell=True,
-                    bufsize=self.buffer_size,
-                    stdin=subprocess.PIPE,
-                    stdout=subprocess.PIPE,
-                    close_fds=True)
-                
-            chunk = proc.stdout.read(self.buffer_size)
-            yield chunk
-            file_out.write(chunk)
-           
-            # Processing
-            while chunk:
-                chunk = proc.stdout.read(self.buffer_size)
+                                         self.source,
+                                         self.metadata,
+                                         self.ext,
+                                         self.cache_dir,
+                                         self.options)
+        except:
+            yield 'ExporterError [3]: pre_process'
+
+        # Processing (streaming + cache writing)
+        try:
+            stream = self.core_process(self.command,self.buffer_size,self.dest)
+            for chunk in stream:
                 yield chunk
-                file_out.write(chunk)           
-           
-            file_out.close()
-            
-            # Encoding
-            # os.system('lame '+args+' --tc "default" "'+self.source+
-            #                        '" "'+self.dest+'"')
-            
-            # Post-proccessing (self)
-            self.write_tags()
+        except:
+            yield 'ExporterError: core_process'
+
+        # Post-proccessing
+        try:
+            self.write_tags()        
             self.post_process(self.item_id,
                          self.source,
                          self.metadata,
                          self.ext,
                          self.cache_dir,
                          self.options)
-                        
-            # Output
-            # return self.dest
-
-        except IOError:
-            yield 'ExporterError [3]: source file does not exist.'
-
+        except:
+            yield 'ExporterError: post_process'
+    
+        # Encoding
+        # os.system('lame '+args+' --tc "default" "'+self.source+
+        #                        '" "'+self.dest+'"')
+            
index e4c402209463679f5adb7ec2fe88fe335bb9e466..6c4a26332029b028c77918e616b1d7c3a3d829e0 100644 (file)
@@ -74,14 +74,9 @@ class OggExporter(ExporterCore):
             media[tag] = str(self.metadata[tag])
         media.save()
 
-
-    def process(self, item_id, source, metadata, options=None):
-        self.item_id = item_id
-        self.source = source
-        self.metadata = metadata
-        self.options = {}
+    def get_args(self,options=None):
+        """Get process options and return arguments for the encoder"""
         args = ''
-        
         if not options is None:
             self.options = options
             
@@ -96,60 +91,55 @@ class OggExporter(ExporterCore):
                 args = args + '-q '+self.options['ogg_quality']
             else:
                 args = args + '-b '+self.bitrate_default
-
         else:
             args = ' -Q -b '+self.bitrate_default
+        return args
             
-        #if os.path.exists(self.source) and not iswav16(self.source):
-        #    self.source = self.decode()
-        
-        # Pre-processing
+
+    def process(self, item_id, source, metadata, options=None):        
+        self.item_id = item_id
+        self.source = source
+        self.metadata = metadata
+        #self.options = {}
+        self.args = self.get_args(options)
         self.ext = self.get_file_extension()
-        self.dest = self.pre_process(self.item_id,
-                                     self.source,
-                                     self.metadata,
-                                     self.ext,
-                                     self.cache_dir,
-                                     self.options)
-       
+        self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \
+                       '| oggenc '+self.args+' -'
+
+        # Pre-proccessing
         try:
-            # Initializing
-            chunk = 0
-            file_out = open(self.dest,'w')
-            
-            proc = subprocess.Popen( \
-                    'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+
-                    '| oggenc '+args+' -',
-                    shell=True,
-                    bufsize=self.buffer_size,
-                    stdin=subprocess.PIPE,
-                    stdout=subprocess.PIPE,
-                    close_fds=True)
-
-            chunk = proc.stdout.read(self.buffer_size)
-            yield chunk
-            file_out.write(chunk)
-
-            # Processing
-            while chunk:
-                chunk = proc.stdout.read(self.buffer_size)
+            self.dest = self.pre_process(self.item_id,
+                                         self.source,
+                                         self.metadata,
+                                         self.ext,
+                                         self.cache_dir,
+                                         self.options)
+        except:
+            yield 'ExporterError [3]: pre_process'
+
+        # Processing (streaming + cache writing)
+        try:
+            stream = self.core_process(self.command,self.buffer_size,self.dest)
+            for chunk in stream:
                 yield chunk
-                file_out.write(chunk)           
-            
-            #file_in.close()
-            file_out.close()
-                
-            # Post-proccessing
-            #os.system('sox "'+self.source+'" -w -r 44100 -t wav -c2 - \
-            #      | oggenc '+args+' -o "'+self.dest+'" -')
-  
-            self.write_tags()
+        except:
+            yield 'ExporterError: core_process'
+
+        # Post-proccessing
+        try:
+            self.write_tags()        
             self.post_process(self.item_id,
-                            self.source,
-                            self.metadata,
-                            self.ext,
-                            self.cache_dir,
-                            self.options)
-               
-        except IOError:
-            yield 'ExporterError [3]: source file does not exist.'
+                         self.source,
+                         self.metadata,
+                         self.ext,
+                         self.cache_dir,
+                         self.options)
+        except:
+            yield 'ExporterError: post_process'
+
+
+
+        # Post-proccessing
+        #os.system('sox "'+self.source+'" -w -r 44100 -t wav -c2 - \
+        #      | oggenc '+args+' -o "'+self.dest+'" -')
+  
index 96e68f0314ceb3fc41a987cbeb18cf7a157bdaa6..06c8f8a6db91d8a85a3c5b1e51d1ec780150fd9f 100644 (file)
@@ -120,7 +120,7 @@ class WavExporter(ExporterCore):
             chunk = 0
             file_in = open(self.source,'rb')
             file_out = open(self.dest,'w')
-        
+
             chunk = file_in.read(self.buffer_size)
             yield chunk
             file_out.write(chunk)
@@ -133,12 +133,6 @@ class WavExporter(ExporterCore):
 
             file_in.close()
             file_out.close()
-
-            #if self.compare_md5_key():
-            #os.system('cp -a "'+self.source+'" "'+ self.dest+'"')
-            #print 'COPIED'
-            
-            # Pre-proccessing (self)
             self.write_tags()
 
             # Create the md5 key
@@ -156,9 +150,12 @@ class WavExporter(ExporterCore):
                          self.cache_dir,
                          self.options)
 
-            # Output                
-            #return self.dest
-
         except IOError:
             yield 'ExporterError [3]: source file does not exist.'
 
+
+
+            #if self.compare_md5_key():
+            #os.system('cp -a "'+self.source+'" "'+ self.dest+'"')
+            #print 'COPIED'
+            
index d6063d62dd736d046a8c9f2fddfec27f5da92851..01d35a8cb09a7caf2f8bf2abfbb2ea3c7c1b9b90 100644 (file)
@@ -27,7 +27,7 @@ metadata = {'identifier': 'Test',  #collection
          'date': '2004',
          'publisher': 'Parisson',
          }
-options = {'verbose': '0'}
+options = {'verbose': '1'}
 
 class ExportTest(Component):
     
@@ -45,6 +45,7 @@ class ExportTest(Component):
 
             for chunk in stream:
                 pass
+                #print chunk
             
 compmgr = ComponentManager()
 test = ExportTest(compmgr)