def run(self, fname): baseFname, ext = os.path.splitext(os.path.basename(fname)) wavfname, created = util.docserver_get_wav_filename(self.musicbrainz_id) panelWidth = 900 # pixels panelHeight = 255 # pixels zoomlevels = [4, 8, 16, 32] # seconds options = coll.namedtuple('options', 'image_height fft_size image_width') options.image_height = panelHeight options.image_width = panelWidth options.fft_size = 31 ret = {} for zoom in zoomlevels: wvFile = wave.Wave_read(wavfname) framerate = wvFile.getframerate() totalframes = wvFile.getnframes() # We want this many frames per file at this zoom level. framesperimage = framerate * zoom wfname = "waveform%s" % zoom specname = "spectrum%s" % zoom wfdata = [] specdata = [] sumframes = 0 while sumframes < totalframes: fp, smallname = tempfile.mkstemp(".wav") os.close(fp) data = wvFile.readframes(framesperimage) wavout = wave.open(smallname, "wb") # This will set nframes, but writeframes resets it wavout.setparams(wvFile.getparams()) wavout.writeframes(data) wavout.close() sumframes += framesperimage specio = StringIO() # Set the name attr so that PIL gets the filetype hint specio.name = "spec.png" wavio = StringIO() wavio.name = "wav.png" w2png.genimages(smallname, wavio, specio, options) os.unlink(smallname) specdata.append(specio.getvalue()) wfdata.append(wavio.getvalue()) ret[wfname] = wfdata ret[specname] = specdata ret["smallfull"] = self.make_mini(wavfname) if created: os.unlink(wavfname) return ret
def make_mini(self, wavfname): smallfulloptions = coll.namedtuple('options', 'image_height fft_size image_width') smallfulloptions.fft_size = 4096 smallfulloptions.image_height = 65 smallfulloptions.image_width = 900 smallfullio = StringIO() smallfullio.name = "wav.png" # We don't use the spectogram, but need to provide it anyway smallfullspecio = StringIO() smallfullspecio.name = "spec.png" w2png.genimages(wavfname, smallfullio, smallfullspecio, smallfulloptions) return smallfullio.getvalue()
def make_mini(self, wavfname): smallfulloptions = coll.namedtuple("options", "image_height fft_size image_width f_min f_max scale_exp pallete") smallfulloptions.fft_size = 4096 smallfulloptions.image_height = 65 smallfulloptions.image_width = 900 smallfulloptions.f_min = None smallfulloptions.f_max = None smallfulloptions.f_max = None smallfulloptions.pallete = None smallfulloptions.scale_exp = None smallfullio = StringIO() smallfullio.name = "wav.png" # We don't use the spectogram, but need to provide it anyway smallfullspecio = StringIO() smallfullspecio.name = "spec.png" w2png.genimages(wavfname, smallfullio, smallfullspecio, smallfulloptions) return smallfullio.getvalue()
def make_mini(self, wavfname): smallfulloptions = coll.namedtuple('options', 'image_height fft_size image_width f_min f_max scale_exp pallete') smallfulloptions.fft_size = 4096 smallfulloptions.image_height = 65 smallfulloptions.image_width = 900 smallfulloptions.f_min = None smallfulloptions.f_max = None smallfulloptions.f_max = None smallfulloptions.pallete = None smallfulloptions.scale_exp = None smallfullio = six.BytesIO() smallfullio.name = "wav.png" # We don't use the spectogram, but need to provide it anyway smallfullspecio = six.BytesIO() smallfullspecio.name = "spec.png" invmfccio = six.BytesIO() invmfccio.name = "spec.png" w2png.genimages(wavfname, smallfullio, smallfullspecio, invmfccio, smallfulloptions) return smallfullio.getvalue()
def run(self, musicbrainzid, fname): baseFname, ext = os.path.splitext(os.path.basename(fname)) wavfname, created = util.docserver_get_wav_filename(musicbrainzid) panelWidth = 900 # pixels panelHeight = 255 # pixels zoomlevels = self._zoom_levels # seconds options = coll.namedtuple("options", "image_height fft_size image_width f_min f_max scale_exp pallete") options.image_height = panelHeight options.fft_size = self._fft_size options.f_min = self._f_min options.f_max = self._f_max options.pallete = self._pallete options.scale_exp = self._scale_exp ret = {} for zoom in zoomlevels: # At the beginning of each zoom level we reset the image_width # since we are modifying it at the end of the last zoom level options.image_width = panelWidth wvFile = wave.Wave_read(wavfname) framerate = wvFile.getframerate() totalframes = wvFile.getnframes() # We want this many frames per file at this zoom level. framesperimage = framerate * zoom wfname = "waveform%s" % zoom specname = "spectrum%s" % zoom wfdata = [] specdata = [] sumframes = 0 while sumframes < totalframes: if sumframes + framesperimage > totalframes: remaining_frames = totalframes - sumframes options.image_width = options.image_width * remaining_frames / framesperimage else: remaining_frames = framesperimage fp, smallname = tempfile.mkstemp(".wav") os.close(fp) data = wvFile.readframes(remaining_frames) wavout = wave.open(smallname, "wb") # This will set nframes, but writeframes resets it wavout.setparams(wvFile.getparams()) wavout.writeframes(data) wavout.close() sumframes += framesperimage specio = StringIO() # Set the name attr so that PIL gets the filetype hint specio.name = "spec.png" wavio = StringIO() wavio.name = "wav.png" w2png.genimages(smallname, wavio, specio, options) os.unlink(smallname) specdata.append(specio.getvalue()) wfdata.append(wavio.getvalue()) ret[wfname] = wfdata ret[specname] = specdata ret["smallfull"] = self.make_mini(wavfname) if created: os.unlink(wavfname) return ret
def run(self, musicbrainzid, fname): baseFname, ext = os.path.splitext(os.path.basename(fname)) wavfname, created = util.docserver_get_wav_filename(musicbrainzid) panelWidth = 900 # pixels panelHeight = 255 # pixels zoomlevels = self._zoom_levels # seconds options = coll.namedtuple( 'options', 'image_height fft_size image_width f_min f_max scale_exp pallete') options.image_height = panelHeight options.fft_size = self._fft_size options.f_min = self._f_min options.f_max = self._f_max options.pallete = self._pallete options.scale_exp = self._scale_exp ret = {} for zoom in zoomlevels: # At the beginning of each zoom level we reset the image_width # since we are modifying it at the end of the last zoom level options.image_width = panelWidth wvFile = wave.Wave_read(wavfname) framerate = wvFile.getframerate() totalframes = wvFile.getnframes() # We want this many frames per file at this zoom level. framesperimage = framerate * zoom wfname = "waveform%s" % zoom specname = "spectrum%s" % zoom inv_mfcc_name = "inv_mfcc_spectrum%s" % zoom wfdata = [] specdata = [] inv_mfcc_data = [] sumframes = 0 while sumframes < totalframes: if sumframes + framesperimage > totalframes: remaining_frames = (totalframes - sumframes) options.image_width = options.image_width * remaining_frames / framesperimage else: remaining_frames = framesperimage fp, smallname = tempfile.mkstemp(".wav") os.close(fp) data = wvFile.readframes(remaining_frames) wavout = wave.open(smallname, "wb") # This will set nframes, but writeframes resets it wavout.setparams(wvFile.getparams()) wavout.writeframes(data) wavout.close() sumframes += framesperimage specio = io.BytesIO() # Set the name attr so that PIL gets the filetype hint specio.name = "spec.png" wavio = io.BytesIO() wavio.name = "wav.png" in_mfcc_io = io.BytesIO() in_mfcc_io.name = "melspec.png" w2png.genimages(smallname, wavio, specio, in_mfcc_io, options) os.unlink(smallname) specdata.append(specio.getvalue()) wfdata.append(wavio.getvalue()) inv_mfcc_data.append(in_mfcc_io.getvalue()) ret[wfname] = wfdata ret[specname] = specdata ret[inv_mfcc_name] = inv_mfcc_data ret["smallfull"] = self.make_mini(wavfname) if created: os.unlink(wavfname) return ret