#!/usr/bin/env python3 # # loopertrx: import/export audio data from some looper pedals. # # Copyright (C) 2017 Reiner Herrmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. import random import struct import sys import argparse import usb.core import usb.util LOOPER_VID = 0x0483 LOOPER_PID = 0x572a ENDPOINT_IN = 0x81 ENDPOINT_OUT = 0x01 COMMAND_SIZE = 0xfe COMMAND_DATA = 0xff def random_tag(): return random.randint(0, 1<<32 - 1) def mass_storage_header(data_len, cdb_len, tag=None): header = "USBC".encode('ascii') if not tag: tag = random_tag() flags = 0x80 target = 0x00 header += struct.pack(' 0: bufsize = (size >= 65536) and 65536 or size # data needs to be transferred in multiples of 1k blocks padding = (1024 - (bufsize % 1024)) % 1024 buf = get_data(dev, bufsize + padding) print('.', end='', flush=True), outfile.write(buf[:bufsize]) size -= bufsize print(" Done.") def transmit_file(dev): with open("/tmp/dump.wav", 'rb') as infile: content = infile.read() tag = random_tag() # skip first 44 bytes for now; we assume valid file. TODO: validate content = content[44:] content_size = len(content) print("Transmitting ", end='', flush=True) while len(content) > 0: buf = content[:65536] padsize = (1024 - (len(buf) % 1024)) % 1024 buf += b'\x00' * padsize send_data(dev, buf, tag) print('.', end='', flush=True), content = content[65536:] submit_data_len(dev, content_size, tag) print(" Done.") def main(): argp = argparse.ArgumentParser() argp.add_argument('action', choices=['rx', 'tx']) args = argp.parse_args() dev = init_device() if args.action == 'rx': receive_file(dev) elif args.action == 'tx': transmit_file(dev) if __name__ == "__main__": main()