2019-12-22 10:26:51 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# A simple example showing how to load and play a sound.
|
2020-01-01 17:00:26 +01:00
|
|
|
# Copyright (C) 2019, 2020 Nguyễn Gia Phong
|
2019-12-22 10:26:51 +01:00
|
|
|
#
|
2020-01-04 05:55:49 +01:00
|
|
|
# This file is part of palace.
|
2019-12-22 10:26:51 +01:00
|
|
|
#
|
2020-01-04 05:55:49 +01:00
|
|
|
# palace is free software: you can redistribute it and/or modify it
|
2019-12-22 10:26:51 +01:00
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License,
|
|
|
|
# or (at your option) any later version.
|
|
|
|
#
|
2020-01-04 05:55:49 +01:00
|
|
|
# palace is distributed in the hope that it will be useful,
|
2019-12-22 10:26:51 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
2020-01-04 05:55:49 +01:00
|
|
|
# along with palace. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-22 10:26:51 +01:00
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from itertools import count, takewhile
|
|
|
|
from sys import stderr
|
|
|
|
from time import sleep
|
|
|
|
from typing import Iterable
|
|
|
|
|
2020-01-05 14:21:47 +01:00
|
|
|
from palace import Device, Context, Buffer
|
2019-12-22 10:26:51 +01:00
|
|
|
|
|
|
|
PERIOD = 0.025
|
|
|
|
|
|
|
|
|
|
|
|
def pretty_time(seconds: float) -> str:
|
|
|
|
"""Return human-readably formatted time."""
|
|
|
|
time = datetime.min + timedelta(seconds=seconds)
|
|
|
|
if seconds < 3600: return time.strftime('%M:%S')
|
|
|
|
return time.strftime('%H:%M:%S')
|
|
|
|
|
|
|
|
|
2019-12-25 16:41:02 +01:00
|
|
|
def play(files: Iterable[str], device: str) -> None:
|
2019-12-22 10:26:51 +01:00
|
|
|
"""Load and play files on the given device."""
|
2020-01-01 17:00:26 +01:00
|
|
|
with Device(device, fail_safe=True) as dev, Context(dev) as ctx:
|
|
|
|
print('Opened', dev.name['full'])
|
|
|
|
for filename in files:
|
|
|
|
try:
|
|
|
|
buffer = Buffer(ctx, filename)
|
|
|
|
except RuntimeError:
|
|
|
|
stderr.write(f'Failed to open file: {filename}\n')
|
|
|
|
continue
|
2020-01-03 10:28:17 +01:00
|
|
|
with buffer, buffer.play() as src:
|
2020-01-01 17:00:26 +01:00
|
|
|
print(f'Playing {filename} ({buffer.sample_type_name},',
|
|
|
|
f'{buffer.channel_config_name}, {buffer.frequency} Hz)')
|
2019-12-22 10:26:51 +01:00
|
|
|
|
2020-01-01 17:00:26 +01:00
|
|
|
invfreq = 1 / buffer.frequency
|
|
|
|
for i in takewhile(lambda i: src.playing, count()):
|
|
|
|
print(f' {pretty_time(src.offset*invfreq)} /'
|
|
|
|
f' {pretty_time(buffer.length*invfreq)}',
|
|
|
|
end='\r', flush=True)
|
|
|
|
sleep(PERIOD)
|
|
|
|
print()
|
2019-12-22 10:26:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument('files', nargs='+', help='audio files')
|
2019-12-25 16:41:02 +01:00
|
|
|
parser.add_argument('-d', '--device', default='', help='device name')
|
2019-12-22 10:26:51 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
play(args.files, args.device)
|