tags: python scipy numpy I'm working through the Numpy Cookbook 2nd Edition, though slightly old, and learning scipy along the way. I'm doing some of this in Jupyter Lab, so I can post notebooks as I go along. # HTML * [LearnNympy_001.html](LearnNumpy_001.html) * [PercLoop_001.html](PercLoop_001.html) Note that this only works with single channel audio for now. Until I've fully explored doing audio stuff with mono, I won't look into stereo and handling e.g. auto mono to stereo conversion. Numpy's shape is a tuple of length 1 for single channel, but is a tuple of length 2 for more channels. The long term goal is to make something that takes lists of events (like Csound's score language), processes it, and outputs either Midi or audio. This experiment is the first foray into the audio part. Next step is sample rate conversion (just using linear interpolation for now), so that I can repitch things. Another next step is basic DSP like filters. Efficiency and runtime aren't a problem provided things don't take many minutes to render for a small input. It's mainly a learning DSP and related from scratch exercise, as I like to know how things work from the ground up. # Notebooks The samples come from sample packs, so I can't share those. * [LearnNumpy_001](LearnNumpy_001.ipynb) * [PercLoop_001](PercLoop_001.ipynb) The `kick1fpm.wav` sample is a kick sample, mono, floating point, at 44100Hz sample rate. I'll convert everything into this format before use for now (using sox and ffmpeg, though there is the simple `tomono.py` example below to show how this is done in Python using Numpy and Scipy). # Misc ## `tomono.py` ```py #!/usr/bin/env python import scipy.io.wavfile as wavfile import numpy as np import sys try: ifn,ofn = sys.argv[1:] except ValueError: print(f"{sys.argv[0]} input_filename output_filename") exit(1) def proc(ifn,ofn): sr, data = wavfile.read(ifn) if np.issubdtype(data.dtype,np.integer): dti = np.iinfo(data.dtype) m = -dti.min M = dti.max m = max(m,M) data = data.astype("float64")/m elif np.issubdtype(data.dtype,np.floating): data = data.astype("float64") else: raise Exception("Neither int nor float") shp = data.shape if len(shp) > 1: nchs = shp[1] mono = data.sum(axis=1)/nchs else: mono = data wavfile.write(ofn,sr,mono) print(f"Written {ofn}") def main(): proc(ifn,ofn) if __name__ == "__main__": main() ```