From 080a9dd8af8cdf303eaa9a93bc4f8571f969960f Mon Sep 17 00:00:00 2001 From: yomguy <> Date: Fri, 11 May 2007 16:08:38 +0000 Subject: [PATCH] Made all exporters generators. Closes: #8 . Input audio raw data is now passed into sox while encoding (no decoding needed anymore...) --- telemeta/export/__init__.py | 6 +++--- telemeta/export/core.py | 1 + telemeta/export/flac.py | 36 ++++++++++++++++++++++++++++++++---- telemeta/export/mp3.py | 5 ++--- telemeta/export/wav.py | 25 ++++++++++++++++++++++--- tests/export_test.py | 4 ++-- 6 files changed, 62 insertions(+), 15 deletions(-) diff --git a/telemeta/export/__init__.py b/telemeta/export/__init__.py index 0e05c996..6ba2ef16 100644 --- a/telemeta/export/__init__.py +++ b/telemeta/export/__init__.py @@ -1,6 +1,6 @@ from telemeta.export.api import * from telemeta.export.core import * -#from telemeta.export.ogg import * -#from telemeta.export.flac import * -#from telemeta.export.wav import * +from telemeta.export.ogg import * +from telemeta.export.flac import * +from telemeta.export.wav import * from telemeta.export.mp3 import * \ No newline at end of file diff --git a/telemeta/export/core.py b/telemeta/export/core.py index 2cbac609..23e0b46c 100644 --- a/telemeta/export/core.py +++ b/telemeta/export/core.py @@ -32,6 +32,7 @@ class ExporterCore(Component): self.dest = '' self.metadata = [] self.cache_dir = 'cache' + self.buffer_size = 0xFFFF def set_cache_dir(self,path): self.cache_dir = path diff --git a/telemeta/export/flac.py b/telemeta/export/flac.py index f2084999..56334e5a 100644 --- a/telemeta/export/flac.py +++ b/telemeta/export/flac.py @@ -12,6 +12,7 @@ import os import string +import subprocess from telemeta.export.core import * from telemeta.export.api import IExporter @@ -31,6 +32,7 @@ class FlacExporter(ExporterCore): self.dest = '' self.quality_default = '5' self.info = [] + self.buffer_size = 0xFFFF def get_format(self): return 'FLAC' @@ -115,9 +117,35 @@ class FlacExporter(ExporterCore): self.cache_dir, self.options) + # Initializing + chunk = 0 + file_out = open(self.dest,'w') + + proc = subprocess.Popen( \ + 'sox "'+self.source+'" -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_out.close() + + # Encoding - os.system('flac '+args+' -o "'+self.dest+'" "'+ \ - self.source+'" > /dev/null') + #os.system('flac '+args+' -o "'+self.dest+'" "'+ \ + # self.source+'" > /dev/null') # Post-proccessing (self) self.write_tags() @@ -129,8 +157,8 @@ class FlacExporter(ExporterCore): self.options) # Output - return self.dest + #return self.dest except IOError: - return 'ExporterError [3]: source file does not exist.' + yield 'ExporterError [3]: source file does not exist.' diff --git a/telemeta/export/mp3.py b/telemeta/export/mp3.py index c32e688b..42beade6 100644 --- a/telemeta/export/mp3.py +++ b/telemeta/export/mp3.py @@ -132,9 +132,8 @@ class Mp3Exporter(ExporterCore): file_out = open(self.dest,'w') proc = subprocess.Popen( \ - #'sox "'+self.source+'" -w -r 44100 -t wav -c2 - '+ - #'| lame '+args+' --tc "default" -', - 'lame '+args+' --tc "default" "'+self.source+'" -', + 'sox "'+self.source+'" -w -r 44100 -t wav -c2 - '+ + '| lame '+args+' --tc "default" - -', shell=True, bufsize=self.buffer_size, stdin=subprocess.PIPE, diff --git a/telemeta/export/wav.py b/telemeta/export/wav.py index 132f8457..96e68f03 100644 --- a/telemeta/export/wav.py +++ b/telemeta/export/wav.py @@ -29,6 +29,7 @@ class WavExporter(ExporterCore): self.source = '' self.dest = '' self.options = {} + self.buffer_size = 0xFFFF def get_format(self): return 'WAV' @@ -115,8 +116,26 @@ class WavExporter(ExporterCore): self.cache_dir, self.options) + # Initializing + 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) + + # Processing + while chunk: + chunk = file_in.read(self.buffer_size) + yield chunk + file_out.write(chunk) + + file_in.close() + file_out.close() + #if self.compare_md5_key(): - os.system('cp -a "'+self.source+'" "'+ self.dest+'"') + #os.system('cp -a "'+self.source+'" "'+ self.dest+'"') #print 'COPIED' # Pre-proccessing (self) @@ -138,8 +157,8 @@ class WavExporter(ExporterCore): self.options) # Output - return self.dest + #return self.dest except IOError: - return 'ExporterError [3]: source file does not exist.' + yield 'ExporterError [3]: source file does not exist.' diff --git a/tests/export_test.py b/tests/export_test.py index cdf7d988..68ca8445 100644 --- a/tests/export_test.py +++ b/tests/export_test.py @@ -17,8 +17,8 @@ from telemeta.core import * from telemeta.core import ComponentManager cache_dir = 'cache/' -source = 'samples/wav/The Chicken-James Brown.wav' -#source = 'samples/wav/Cellar - Show Me - 02.wav' +#source = 'samples/wav/The Chicken-James Brown.wav' +source = 'samples/wav/Cellar - Show Me - 02.wav' item_id = '10' metadata = {'identifier': 'Test', #collection 'title': 'Show Me', -- 2.39.5