Skip to content

Commit

Permalink
checkpoint for working wire protocol sample transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
bwhitman committed Oct 3, 2024
1 parent 3e4731c commit 7ad36f6
Show file tree
Hide file tree
Showing 13 changed files with 936 additions and 502 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ all: default

SOURCES = src/algorithms.c src/amy.c src/envelope.c src/examples.c \
src/filters.c src/oscillators.c src/pcm.c src/partials.c src/custom.c \
src/delay.c src/log2_exp2.c src/patches.c
src/delay.c src/log2_exp2.c src/patches.c src/transfer.c

OBJECTS = $(patsubst %.c, %.o, src/algorithms.c src/amy.c src/envelope.c \
src/delay.c src/partials.c src/custom.c src/patches.c \
src/examples.c src/filters.c src/oscillators.c src/pcm.c src/log2_exp2.c \
src/libminiaudio-audio.c)

src/libminiaudio-audio.c src/transfer.c)
HEADERS = $(wildcard src/*.h) src/amy_config.h
HEADERS_BUILD := $(filter-out src/patches.h,$(HEADERS))

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Here's the full list:
| `w` | `wave` | uint 0-11 | Waveform: [0=SINE, PULSE, SAW_DOWN, SAW_UP, TRIANGLE, NOISE, KS, PCM, ALGO, PARTIAL, PARTIALS, BYO_PARTIALS, OFF]. default: 0/SINE |
| `x` | `eq` | float,float,float | Equalization in dB low (~800Hz) / med (~2500Hz) / high (~7500Gz) -15 to 15. 0 is off. default 0. |
| `X` | `eg1_type` | uint 0-3 | Type for Envelope Generator 1 - 0: Normal (RC-like) / 1: Linear / 2: DX7-style / 3: True exponential. |
| `z` | `load_sample` | uint x 6 | Signal to start loading sample. patch, length(samples), samplerate, midinote, loopstart, loopend. All subsequent messages are base64 encoded WAVE-style frames of audio until `length` is reached. |



Expand Down
43 changes: 42 additions & 1 deletion amy.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def message(**kwargs):
kw_map = {'osc': 'vI', 'wave': 'wI', 'note': 'nF', 'vel': 'lF', 'amp': 'aC', 'freq': 'fC', 'duty': 'dC', 'feedback': 'bF', 'time': 'tI',
'reset': 'SI', 'phase': 'PF', 'pan': 'QC', 'client': 'cI', 'volume': 'vF', 'pitch_bend': 'sF', 'filter_freq': 'FC', 'resonance': 'RF',
'bp0': 'AL', 'bp1': 'BL', 'eg0_type': 'TI', 'eg1_type': 'XI', 'debug': 'DI', 'chained_osc': 'cI', 'mod_source': 'LI', 'clone_osc': 'CI',
'eq': 'xL', 'filter_type': 'GI', 'algorithm': 'oI', 'ratio': 'IF', 'latency_ms': 'NI', 'algo_source': 'OL',
'eq': 'xL', 'filter_type': 'GI', 'algorithm': 'oI', 'ratio': 'IF', 'latency_ms': 'NI', 'algo_source': 'OL', 'load_sample': 'zL',
'chorus': 'kL', 'reverb': 'hL', 'echo': 'ML', 'load_patch': 'KI', 'store_patch': 'uS', 'voices': 'rL',
'external_channel': 'WI', 'portamento': 'mI',
'patch': 'pI', 'num_partials': 'pI', # Note alaising.
Expand Down Expand Up @@ -309,7 +309,48 @@ def stop():
def restart():
import libamy
libamy.restart()

def transfer_wav(wavfilename, patch=1024, midinote=0, loopstart=0, loopend=0):
from math import ceil
import amy_wave # our version of a wave file reader that looks for sampler metadata
# tulip has ubinascii, normal has base64
try:
import base64
def b64(b):
return base64.b64encode(b)
except ImportError:
import ubinascii
def b64(b):
return ubinascii.b2a_base64(b)[:-1]

w = amy_wave.open(wavfilename, 'r')

if(w.getnchannels()>1):
# de-interleave and just choose the first channel
f = bytes([f[j] for i in range(0,len(f),4) for j in (i,i+1)])
if(loopstart==0):
if(hasattr(w,'_loopstart')):
loopstart = w._loopstart
if(loopend==0):
if(hasattr(w,'_loopend')):
loopend = w._loopend
if(midinote==0):
if(hasattr(w,'_midinote')):
midinote = w._midinote
else:
midinote=60

# Tell AMY we're sending over a sample
s = "%d,%d,%d,%d,%d,%d" % (patch, w.getnframes(), w.getframerate(), midinote, loopstart, loopend)
send(load_sample=s)
# Now generate the base64 encoded segments, 188 bytes / 94 frames at a time
# why 188? that generates 252 bytes of base64 text. amy's max message size is currently 255.
for i in range(ceil(w.getnframes()/94)):
message = b64(w.readframes(94))
send_raw(message.decode('ascii'))
print("Loaded sample over wire protocol. Patch #%d. %d bytes, %d frames, midinote %d" % (patch, w.getnframes()*2, w.getnframes(), midinote))


"""
Convenience functions
"""
Expand Down
Loading

0 comments on commit 7ad36f6

Please sign in to comment.