Feature Creep 2026, 38 - Audio Test script

I wanted to experiment with bitrates and audio a little. First though, I have to figure out what I can do to an audio file on a machine-level. Because I will want to interfere with the file, I think it would be sensible to figure out how to generate an audio file by machine, and also how to alter it by machine.

import numpy as np
import wavio

rate = 44100
T = 20
f = 440.0

t = np.linspace(0, T, T*rate, endpoint=False)
x = np.sin(2*np.pi*f*t)

wavio.write("sine440.wav", x, rate, sampwidth=3)

This code snippet generates a twenty-second 44.1kHz sampled 440 A note. Now, I actually want to introduce some parameters to make it a "spicy" 440 A note. One is the "failure rate" which randomly flips a boolean switch as to whether to alter the value at the linspace, and the other is a random modifier between -1 and 1. The first parameter I would want to be easily tunable, which, for me, tends to call for argparse. Finally, because I want to look at what exactly I'm listening to, I would also like the sine graph that I ended up with in the end.

import numpy as np
import wavio
import argparse as argp
import random

def add_spice(spiciness, f, t):
  if (random.random() < spiciness):
    return np.sin(2*np.pi*f*t) + random.randrange(-1, 1)
  return np.sin(2*np.pi*f*t)

if __name__ == "__main__":
  parser = argp.ArgumentParser(
    prog='Spicy Sine',
    description='Generates not-quite 440Hz A')
  parser.add_argument('-s', '--spiciness', default=0)

  args = parser.parse_args()

  rate = 44100
  T = 20
  f = 440.0

  spicy = 0
  spiciness = args.spiciness

  t = np.linspace(0, T, T*rate, endpoint=False)
  x = add_spice(spiciness, f, t)

  wavio.write("sine440.wav", x, rate, sampwidth=3)
Next
Next

Feature Creep 2026, 37 - Some Bash Scripting