def _callback_post_convert(results): # Add sources for all converted files for i, conversion in enumerate(conversions): if results[i][0]: (format, fmt_path) = conversion source = DataSource() source.type = format source.path = ensure_forwardslashes(os.path.basename(fmt_path)) source.size = os.path.getsize(fmt_path) sound_bank.data_sources.append(source) self._append_output_paths([fmt_path]) # Template the results ds = [] (json_template, js_template) = _get_soundbank_template_paths() # Generate JSON ds.append(self._run_task_async(MakoTemplateTask( self.build_env, json_path, json_template, { 'bank': sound_bank, }))) # Generate JS ds.append(self._run_task_async(MakoTemplateTask( self.build_env, js_path, js_template, { 'bank': sound_bank, }))) self._chain(ds)
def _callback_post_merge(cues): sound_bank.cues.extend(cues) # Setup default source (the wav) source = DataSource() source.type = 'audio/wav' source.path = ensure_forwardslashes(os.path.basename(wav_path)) source.size = os.path.getsize(wav_path) sound_bank.data_sources.append(source) # Convert wav to self.formats dc = [] for i, format in enumerate(self.rule.formats): fmt_path = os.path.splitext(wav_path)[0] + _MIME_EXTS[format] d = self._encode_audio_file(wav_path, fmt_path, format) if d: conversions.append((format, fmt_path)) dc.append(d) if len(dc): d = anvil.async.gather_deferreds(dc) d.add_callback_fn(_callback_post_convert) self._chain_errback(d) else: _callback_post_convert([])
def _optimize_image(self, src_path, json_path, js_path): """Begins tasks to identify, optimize, and write metadata for a single input image. Args: src_path: Source path to an image file. json_path: JSON file output path. js_path: JS file output path. Returns: A list of Deferreds for all image tasks. """ ds = [] rel_src_path = anvil.util.strip_build_paths( os.path.relpath(src_path, self.build_env.root_path)) rel_json_path = anvil.util.strip_build_paths( os.path.relpath(json_path, self.build_env.root_path)) # TODO(benvanik): want to split this into tasks: # - IdentifyImageTask: read metadata, decide what to do # - *ExecutionTask[]: various conversions (if required) # - Gather back, write metadata # TODO(benvanik): identify in a task (width, height, channels) = self._identify_image(src_path) # TODO(benvanik): encode? drop invalid name things? (whitespace/etc) class_name = os.path.splitext(os.path.basename(src_path))[0] # TODO(benvanik): proper image info construction class Image(object): pass image = Image() image.class_name = '%s.%s' % (self.rule.namespace, class_name) image.friendly_name = class_name image.src_path = rel_src_path image.base_path = ensure_forwardslashes( os.path.dirname(rel_src_path)) image.json_path = ensure_forwardslashes(rel_json_path) image.width = width image.height = height image.channels = channels image.slot_size = \ self.rule.slot_size.split('x') if self.rule.slot_size else None # TODO(benvanik) proper mime type mime_type = { '.png': 'image/png', '.jpg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', }[os.path.splitext(src_path)[1]] class ImageLod(object): pass lod0 = ImageLod() lod0.type = mime_type lod0.path = ensure_forwardslashes(os.path.basename(src_path)) lod0.size = os.path.getsize(src_path) image.lod_list = [ [ lod0, ], ] # TODO(benvanik): optimize, instead of just copying source self._append_output_paths([src_path]) (json_template, js_template) = _get_template_paths() # Generate JSON ds.append( self._run_task_async( MakoTemplateTask(self.build_env, json_path, json_template, { 'image': image, }))) # Generate JS ds.append( self._run_task_async( MakoTemplateTask(self.build_env, js_path, js_template, { 'image': image, }))) return ds
def _optimize_image(self, src_path, json_path, js_path): """Begins tasks to identify, optimize, and write metadata for a single input image. Args: src_path: Source path to an image file. json_path: JSON file output path. js_path: JS file output path. Returns: A list of Deferreds for all image tasks. """ ds = [] rel_src_path = anvil.util.strip_build_paths( os.path.relpath(src_path, self.build_env.root_path)) rel_json_path = anvil.util.strip_build_paths( os.path.relpath(json_path, self.build_env.root_path)) # TODO(benvanik): want to split this into tasks: # - IdentifyImageTask: read metadata, decide what to do # - *ExecutionTask[]: various conversions (if required) # - Gather back, write metadata # TODO(benvanik): identify in a task (width, height, channels) = self._identify_image(src_path) # TODO(benvanik): encode? drop invalid name things? (whitespace/etc) class_name = os.path.splitext(os.path.basename(src_path))[0] # TODO(benvanik): proper image info construction class Image(object): pass image = Image() image.class_name = '%s.%s' % (self.rule.namespace, class_name) image.friendly_name = class_name image.src_path = rel_src_path image.base_path = ensure_forwardslashes(os.path.dirname(rel_src_path)) image.json_path = ensure_forwardslashes(rel_json_path) image.width = width image.height = height image.channels = channels image.slot_size = \ self.rule.slot_size.split('x') if self.rule.slot_size else None # TODO(benvanik) proper mime type mime_type = { '.png': 'image/png', '.jpg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', }[os.path.splitext(src_path)[1]] class ImageLod(object): pass lod0 = ImageLod() lod0.type = mime_type lod0.path = ensure_forwardslashes(os.path.basename(src_path)) lod0.size = os.path.getsize(src_path) image.lod_list = [ [lod0,], ] # TODO(benvanik): optimize, instead of just copying source self._append_output_paths([src_path]) (json_template, js_template) = _get_template_paths() # Generate JSON ds.append(self._run_task_async(MakoTemplateTask( self.build_env, json_path, json_template, { 'image': image, }))) # Generate JS ds.append(self._run_task_async(MakoTemplateTask( self.build_env, js_path, js_template, { 'image': image, }))) return ds
def begin(self): super(AudioTrackListRule._Context, self).begin() # TODO(benvanik): split into tasks # - for each input track: # - for each target format: # - convert audio file # - gather all converted file info # - write templated files js_path = self._get_gen_path(suffix='.js') json_path = self._get_out_path(suffix='.json') self._ensure_output_exists(os.path.dirname(js_path)) self._ensure_output_exists(os.path.dirname(json_path)) self._append_output_paths([js_path, json_path]) base_path = os.path.relpath(self._get_rule_path(), self.build_env.root_path) rel_json_path = anvil.util.strip_build_paths( os.path.relpath(json_path, self.build_env.root_path)) track_list = TrackList() track_list.class_name = self.rule.class_name track_list.friendly_name = \ self.rule.class_name[self.rule.class_name.rfind('.') + 1:] track_list.base_path = ensure_forwardslashes(base_path) track_list.json_path = ensure_forwardslashes(rel_json_path) track_list.tracks = [] # TODO(benvanik): convert/etc to self.formats for src_path in self.src_paths: self._append_output_paths([src_path]) track = Track() track.name = os.path.splitext(os.path.basename(src_path))[0] track.duration = self._get_duration(src_path) track.data_sources = [] # TODO(benvanik) proper mime type mime_type = { '.mp3': 'audio/mpeg', '.ogg': 'audio/ogg', '.wav': 'audio/wav', '.m4a': 'audio/mp4', }[os.path.splitext(src_path)[1]] source = DataSource() source.type = mime_type source.path = ensure_forwardslashes(os.path.relpath(src_path, base_path)) source.size = os.path.getsize(src_path) track.data_sources.append(source) track_list.tracks.append(track) # Template the results ds = [] (json_template, js_template) = _get_tracklist_template_paths() # Generate JSON ds.append(self._run_task_async(MakoTemplateTask( self.build_env, json_path, json_template, { 'list': track_list, }))) # Generate JS ds.append(self._run_task_async(MakoTemplateTask( self.build_env, js_path, js_template, { 'list': track_list, }))) self._chain(ds)
def begin(self): super(AudioSoundbankRule._Context, self).begin() # TODO(benvanik): split into tasks # - for each input file: # - convert to wav if needed # - get info # - append to bank file # - for each target format: # - convert bank file # - gather all converted file info # - write templated files js_path = self._get_gen_path(suffix='.js') json_path = self._get_out_path(suffix='.json') wav_path = self._get_out_path(suffix='.wav') self._ensure_output_exists(os.path.dirname(js_path)) self._ensure_output_exists(os.path.dirname(json_path)) self._ensure_output_exists(os.path.dirname(wav_path)) self._append_output_paths([js_path, json_path, wav_path]) base_path = os.path.relpath(self._get_rule_path(), self.build_env.root_path) rel_json_path = anvil.util.strip_build_paths( os.path.relpath(json_path, self.build_env.root_path)) sound_bank = SoundBank() sound_bank.class_name = self.rule.class_name sound_bank.friendly_name = \ self.rule.class_name[self.rule.class_name.rfind('.') + 1:] sound_bank.base_path = ensure_forwardslashes(base_path) sound_bank.json_path = ensure_forwardslashes(rel_json_path) sound_bank.data_sources = [] sound_bank.cues = [] # TODO(benvanik): run over sources and convert to wav where required # Prepare conversion map # This will hold information about the conversions that will take place conversions = [] def _callback_post_convert(results): # Add sources for all converted files for i, conversion in enumerate(conversions): if results[i][0]: (format, fmt_path) = conversion source = DataSource() source.type = format source.path = ensure_forwardslashes(os.path.basename(fmt_path)) source.size = os.path.getsize(fmt_path) sound_bank.data_sources.append(source) self._append_output_paths([fmt_path]) # Template the results ds = [] (json_template, js_template) = _get_soundbank_template_paths() # Generate JSON ds.append(self._run_task_async(MakoTemplateTask( self.build_env, json_path, json_template, { 'bank': sound_bank, }))) # Generate JS ds.append(self._run_task_async(MakoTemplateTask( self.build_env, js_path, js_template, { 'bank': sound_bank, }))) self._chain(ds) def _callback_post_merge(cues): sound_bank.cues.extend(cues) # Setup default source (the wav) source = DataSource() source.type = 'audio/wav' source.path = ensure_forwardslashes(os.path.basename(wav_path)) source.size = os.path.getsize(wav_path) sound_bank.data_sources.append(source) # Convert wav to self.formats dc = [] for i, format in enumerate(self.rule.formats): fmt_path = os.path.splitext(wav_path)[0] + _MIME_EXTS[format] d = self._encode_audio_file(wav_path, fmt_path, format) if d: conversions.append((format, fmt_path)) dc.append(d) if len(dc): d = anvil.async.gather_deferreds(dc) d.add_callback_fn(_callback_post_convert) self._chain_errback(d) else: _callback_post_convert([]) # Merge all wav files into one, return the list of cues for further # processing in the callback if len(self.src_paths): d = self._run_task_async(_MergeWavesTask( self.build_env, self.src_paths, wav_path)) d.add_callback_fn(_callback_post_merge) self._chain_errback(d) else: _callback([])