def __init__(self,
audio_file: Path,
id: Optional[str] = None,
sample_rate_to_convert_to: int = 16000,
label: Optional[str] = "nolabel",
fourier_window_length: int = 512,
hop_length: int = 128,
mel_frequency_count: int = 128,
label_with_tags: str = None,
positional_label: Optional[PositionalLabel] = None):
# The default values for hop_length and fourier_window_length are powers of 2 near the values specified in the wave2letter paper.
if id is None:
id = name_without_extension(audio_file)
self.audio_file = audio_file
super().__init__(
id=id, get_raw_audio=lambda: librosa.load(str(self.audio_file), sr=self.sample_rate)[0],
label=label, sample_rate=sample_rate_to_convert_to,
fourier_window_length=fourier_window_length, hop_length=hop_length, mel_frequency_count=mel_frequency_count,
label_with_tags=label_with_tags, positional_label=positional_label)
python类Path()的实例源码
def load(corpus_csv_file: Path,
sampled_training_example_count: Optional[int] = None) -> 'Corpus':
import csv
with corpus_csv_file.open(encoding='utf8') as opened_csv:
reader = csv.reader(opened_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
def to_absolute(audio_file_path: Path) -> Path:
return audio_file_path if audio_file_path.is_absolute() else Path(
corpus_csv_file.parent) / audio_file_path
examples = [
(
LabeledExampleFromFile(
audio_file=to_absolute(Path(audio_file_path)), id=id, label=label,
positional_label=None if positional_label == "" else PositionalLabel.deserialize(
positional_label)), Phase[phase])
for id, audio_file_path, label, phase, positional_label in reader]
return Corpus(training_examples=[e for e, phase in examples if phase == Phase.training],
test_examples=[e for e, phase in examples if phase == Phase.test],
sampled_training_example_count=sampled_training_example_count)
def init_hotword_switch(self):
try:
parent_dir = os.path.dirname(TOP_DIR)
snowboyDetectFile = Path(os.path.join(
parent_dir, "hotword_engine/snowboy/_snowboydetect.so"))
print(snowboyDetectFile)
if not snowboyDetectFile.exists():
self.snowboy_switch.set_sensitive(False)
config['hotword_engine'] = 'PocketSphinx'
except Exception as e:
logging.error(e)
config['hotword_engine'] = 'PocketSphinx'
if config['hotword_engine'] == 'Snowboy':
self.snowboy_switch.set_active(True)
else:
self.snowboy_switch.set_active(False)
def request_hotword_choice():
""" Method to request user for default Hotword Engine and configure it in settings.
"""
try:
print("Checking for Snowboy Availability...")
snowboyDetectFile = Path("main/hotword_engine/snowboy/_snowboydetect.so")
if snowboyDetectFile.exists():
print("Snowboy is available on this platform")
choice = input("Do you wish to use Snowboy as default Hotword Detection Engine (Recommended). (y/n) ")
if choice == 'y':
config['hotword_engine'] = 'Snowboy'
print('\nSnowboy set as default Hotword Detection Engine\n')
else:
config['hotword_engine'] = 'PocketSphinx'
print('\nPocketSphinx set as default Hotword Detection Engine\n')
except Exception:
print("Some Error Occurred. Using PocketSphinx as default engine for Hotword. Run this script again to change")
config['hotword_engine'] = 'PocketSphinx'
def create_potree_page(work_dir, server_url, tablename, column):
'''Create an html demo page with potree viewer
'''
# get potree build
potree = os.path.join(work_dir, 'potree')
potreezip = os.path.join(work_dir, 'potree.zip')
if not os.path.exists(potree):
download('Getting potree code', 'http://3d.oslandia.com/potree.zip', potreezip)
# unzipping content
with ZipFile(potreezip) as myzip:
myzip.extractall(path=work_dir)
tablewschema = tablename.split('.')[-1]
sample_page = os.path.join(work_dir, 'potree-{}.html'.format(tablewschema))
abs_sample_page = str(Path(sample_page).absolute())
pending('Creating a potree demo page : file://{}'.format(abs_sample_page))
resource = '{}.{}'.format(tablename, column)
server_url = server_url.replace('http://', '')
with io.open(sample_page, 'wb') as html:
html.write(potree_page.format(resource=resource, server_url=server_url).encode())
ok()
def create_cesium_page(work_dir, tablename, column):
'''Create an html demo page with cesium viewer
'''
cesium = os.path.join(work_dir, 'cesium')
cesiumzip = os.path.join(work_dir, 'cesium.zip')
if not os.path.exists(cesium):
download('Getting cesium code', 'http://3d.oslandia.com/cesium.zip', cesiumzip)
# unzipping content
with ZipFile(cesiumzip) as myzip:
myzip.extractall(path=work_dir)
tablewschema = tablename.split('.')[-1]
sample_page = os.path.join(work_dir, 'cesium-{}.html'.format(tablewschema))
abs_sample_page = str(Path(sample_page).absolute())
pending('Creating a cesium demo page : file://{}'.format(abs_sample_page))
resource = '{}.{}'.format(tablename, column)
with io.open(sample_page, 'wb') as html:
html.write(cesium_page.format(resource=resource).encode())
ok()
def download(self, local_dir_=None, url_=None):
'''
Args:
local_dir_: where to save downloaded file
url_: where to download dataset, if None, use default 'http://yann.lecun.com/exdb/mnist/'
'''
# TODO check whether file exists
if url_ is None:
url_ = 'http://yann.lecun.com/exdb/mnist/'
if local_dir_ is None:
local_dir = self.DEFAULT_DIR
else:
local_dir = Path(local_dir_)
local_dir.mkdir(parents=True, exist_ok=True)
in_filename = '%(subset)s-%(type_s)s-idx%(ndim)s-ubyte.gz'
for subset, (type_s, ndim) in product(
('train', 't10k'), zip(('images', 'labels'), (3,1))):
filename = in_filename % locals()
urllib.request.urlretrieve( url_ + filename, str(local_dir / filename))
def __init__(self, config):
self.config = config
self.ui = None
self.data = {}
self.order = []
self.player = None
self.start_segment = None
self.start_line = 0
self.end_segment = None
self.song_data = {}
self.last_note = None
self.playing_row = None
self.project_dir = Path(config["instance"]["project_dir"])
self.changed = False
self.last_bpm_tap = ""
self.last_nudge = ""
def scan(self):
self.import_file = self.config["instance"]["import-file"]
self.bits = self.import_lister.get(self.import_file)
self.data_file = Path(self.bits["metadata"]).with_suffix(".data")
self.metadata = ConfigParser(inline_comment_prefixes=None)
self.metadata.read(str(self.bits["metadata"]))
if self.data_file.exists():
with self.data_file.open(newline="") as csvfile:
data_reader = csv.DictReader(csvfile, dialect=ImportCsvDialect)
for row in data_reader:
location = float(row["location"])
row["location"] = location
self.data[location] = row
if len(self.data) == 0:
self.add_row(0.0, mark="START")
self.add_row(self.bits["length_secs"], mark="END")
self.update_order()
self.clean()
def backup(self):
dsecs = self.data_file.stat().st_mtime
meta_file = Path(self.bits["metadata"])
msecs = meta_file.stat().st_mtime
secs = max(dsecs, msecs)
suffix = filename_iso_time(secs)
backup_data = self.data_file.with_name("{}-{}{}".format(self.data_file.stem, suffix, self.data_file.suffix))
backup_meta = meta_file.with_name("{}-{}{}".format(meta_file.stem, suffix, meta_file.suffix))
with backup_data.open("w", newline='') as csvfile:
fieldnames = ['location', 'lyric', 'mark', 'track-change', "chord-change", "chord-selection", "note"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore', dialect=ImportCsvDialect)
writer.writeheader()
for location in self.order:
writer.writerow(self.data[location])
with backup_meta.open('w') as meta:
self.metadata.write(meta)
def load_home_config(args=None):
""" Load the user-specific configuration.
It can not contain the `project` section. It is based upon the
application's internal defaults. (Note: It will modified with project
specific settings before it is used.)
"""
global config
global home_dir
if home_dir is None:
if args is not None and args.user_config is not None:
home_dir = Path(args.user_config)
else:
home_dir = Path("~/.bittyband").expanduser()
home_dir.mkdir(parents=True, exist_ok=True)
home_config = home_dir / "settings.conf"
if home_config.exists():
config.read(str(home_config))
if "project" in config:
config["project"].clear()
if "instance" in config:
config["instance"].clear()
return config
def get_project_root():
""" Return the common base-directory for projects.
This allows you to specify a `--project` on the command-line
and to reference a project directory off of this root without adding
a mapping in the `projects` section of the user configuration file.
The value for this comes from a `root` symlink in the `--user-config`
directory. If this is a file, instead of a directory, it needs to
be the name of the directory. (It does '~' expansion.)
The expectation is that this will allow a common `~/BittyProjects`
or `~/Documents/BittyTunes` directory to root your projects.
"""
global home_dir
root = home_dir / "root"
if root.is_file():
t = root.read_text().strip()
root = Path(t).expanduser()
if root.is_dir():
return root.resolve()
return None
def find_project_dir(project):
if project is None:
project = "."
root = get_project_root()
if root is None:
root = Path()
project_dir = Path(project)
if "." == project:
project_dir = Path(".")
elif "/" not in project and "\\" not in project:
if "projects" in config:
if project in config["projects"]:
project_dir = Path(config["projects"].get(project))
project_dir = root.joinpath(project_dir.expanduser())
else:
project_dir = project_dir.expanduser()
if not project_dir.exists():
project_dir = root.joinpath(project_dir)
if project_dir.is_dir():
project_dir = project_dir.resolve()
return project_dir
def __init__(self, cwd=os.curdir):
self._data = []
self.working_directory = Path(cwd)
def _process_str_paths(self, paths):
# TODO: Dies if a file doesn't exist
return [Path(file).resolve() for file in paths]
def add(self, *args):
added = 0
for pattern in args:
try:
files = self._glob(pattern)
files = self._process_str_paths(files)
except NotImplementedError:
# self._glob says "Non-relative patterns are unsupported"
# Check if it exists and add it
files = Path(pattern)
if files.exists():
files = [files]
else:
files = None
if not files:
# TODO: Proper handling of this?
print('Include pattern did not match:', pattern)
else:
for file in files:
if file not in self._data:
self._data.append(file)
added += 1
return added
def _get_gitinfo():
import pygrunt.platform as platform
import subprocess
from pathlib import Path
git = platform.current.find_executable('git')
if git is None:
# No git installed; assume we're on master
return ('master', '')
cwd = str(Path(__file__).parent)
args = [git, 'rev-parse', '--abbrev-ref', 'HEAD']
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=cwd, universal_newlines=True)
if result.returncode != 0:
# Quietly return defaults on fail
return ('master', '')
branch = result.stdout
args = [git, 'rev-parse', 'HEAD']
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=cwd, universal_newlines=True)
if result.returncode != 0:
# Quietly return defaults on fail
return ('master', '')
commit = result.stdout
return (branch.strip(), commit.strip())
def sanitize(self):
import os.path
if not self.working_dir:
self.working_dir = os.path.curdir
if not self.output_dir:
self.output_dir = os.path.join(self.working_dir, 'build', '')
allowed_types = ['executable', 'library', 'shared']
if self.type not in allowed_types:
# TODO: exceptions?
Style.error('Invalid output type:', self.type)
Style.error('Allowed types:', allowed_types)
self.type = allowed_types[0]
Style.error('Reverting to', self.type)
if not self.executable:
self.executable = os.path.join(self.output_dir, self.name)
if self.type == 'executable':
self.executable = platform.current.as_executable(self.executable)
elif self.type == 'library':
self.executable = platform.current.as_static_library(self.executable)
elif self.type == 'shared':
self.executable = platform.current.as_shared_library(self.executable)
self.working_dir = os.path.realpath(self.working_dir)
self.output_dir = os.path.realpath(self.output_dir)
self.sources.working_directory = Path(self.working_dir)
self._init_hooks()
# Stage hooks
# Create an empty list for each stage
def compile_object(self, in_file, out_file):
import subprocess
in_file = Path(in_file)
out_file = Path(out_file)
# Skip compile if RecompileStrategy says so
# Since preprocess_source ( possibly used by recompile ) also modifies self._args,
# we gotta back it up
# TODO: Maybe use something more elegant than self._args?
old_args = self._args
if out_file.is_file():
if not self.recompile.should_recompile(str(in_file)):
# Style.info('Nothing to do with', in_file)
return True
self._args = old_args
Path(out_file).parent.mkdir(parents=True, exist_ok=True)
self._args.extend(self._build_compiler_flags())
result = subprocess.run(self._args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
# TODO: do something useful with output
if result.stdout:
print(result.stdout)
if result.stderr:
print(result.stderr)
return result.returncode == 0
def link_executable(self, in_files, out_file):
import subprocess
Path(out_file).parent.mkdir(parents=True, exist_ok=True)
self._args.extend(self._build_linker_flags())
result = subprocess.run(self._args)
return result.returncode == 0