====== Simulating Analog Audio Cicuits ====== {{wiki:spicesound.png?250 }} Problem: been reading [[http://linuxaudio.org/pipermail/linux-audio-user/2007-May/045851.html|LAU]] Idea: feed the //great// [[sf>ngspice|SPICE]] simulator with audio-data. Solution: add a //libsndfile// voltage-source to SPICE ;) ===== general information ===== A new version of the software and this document is now available at [[:oss:spicesound:start|SpiceSound]] It's yet a quick experiment, and requires patching the SPICE source-code. Initial tests show that it might be realistic to simulate simple schematics in real-time((simulating 1sec audio of the foxx-effect at 48ksps on a intel core P6 1.6GHz takes about 4sec)). Although the spice command is already named ''jack'' it's yet only a file-reader. For testing, there is a tool ''txt2snd'' to convert spice ''.print'' lines into wav files ((txt2snd writes //yet// only 16-bit signed-int-LE wave files)). one (of the many) issues is running spice in audio-samplerate time-steps... - **it's work in progress**. ===== prepare ===== * patch spice: {{wiki:ng-spice-rework-17-sndfile.diff}} * compile {{wiki:txt2snd-0.1.tgz}} * prepare some libsndfile readable audio file. ===== spice syntax ===== The patched spice knows a new type of voltages-source ''jack'' with the optional argument ''file''. an example netlist entry connecting the sound-source ''/tmp/test.wav'' to pin 4(Signal) and pin 0 (GND) with a range of [-1..+1] Volts looks like: V_V2 4 0 file /tmp/test.wav jack 0 0 1.0 0 **Syntax:** jack file * '''' an integer identifier 0 to 2 * '''' offset added in Volts * '''' the multiplier when converting sndfile-float [0..1] to Volts * '''' offset in milliseconds added when seeking the sound-file * NOTE: The option ''file'' must precede the option ''jack'' to take affect. * default files are id=0:''/tmp/test.wav'' id=1:''/tmp/test1.wav'' id=2:''NULL'' ===== txt2snd ===== ''txt2snd'' allows to convert spice output int wav files - it's yet rather simple but has a built-in help. It reads 4 tabulator separated values per line in the format: example: 11 8.916652e-05 1.846313e-02 -7.477363e-01 this format can easily be generated by SPICE. in the following example 20 seconds of ''net 5'' are printed at 48kSPS: .print tran v(4) v(5) .tran 2.08333e-05 20.0 0 2.08333e-05 .op Note: Rounding (spice-milliseconds to audio-sample) is buggy and txt2snd includes an only sometimes working workaround (use ''--verbose'' to investigate skipped/missing frames). the following script might come in handy.. #!/bin/sh INFILE=example.netlist OUTFILE=/tmp/spice.txt WAVFILE=/tmp/spice.wav echo "# writing data to $OUTFILE" ngspice -b $INFILE | awk 'BEGIN{go=0;} /^-------------------------------------/{go=1;} /^[0-9]/{if (go==1) print $0;}' > $OUTFILE echo "# txt to snd" cat $OUTFILE | txt2snd $WAVFILE echo "# done." ====== Example ====== **spice3 sound example ** V_V2 1 0 file /tmp/test.wav jack 0 0 1.0 0 R_R1 1 0 1M .print tran v(1) v(1) .tran 2.08333e-05 2.0 0 2.08333e-05 .op .END Save this example script to ''example.net''; save a 48k wav file to ''/tmp/test/wav'' and launch with ''ngspice -b example.net''. this prints the values (and some spice debug output) to stdout (see script above). pipe the cleaned spice output into ''txt2wav'' to produce a wav file again (also see script above). ** foxxfuzz ** {{wiki:foxx.oregano.gz}} is modeled after: * http://www.geocities.com/CollegePark/Library/1355/tonemachine.gif * http://www.geofex.com/FX_images/foxxfuzz.gif NOTEs: * The simulated Diodes and NPNs are not the original, but the default models available with //oregano//. * the gain/sustain pot is modeled in R50/R51 - sum 50k * the tone pot is modeled in R60/R61 - sum 50k * flip the octave switch, by removing diode D1. w/o Diode = off Beware: it's not yet debugged... Stay tuned for a SPICE netlist and soundfiles, tweaks & screenshots. ** aliki/jace ** ToDo - a good way to verify.. * measure response of a simple circuit and document with aliki (and use in realtime with jace..) * compare aliki w/ spice impulse response analysis. * compare spice processed audio and jace processed audio. ====== References ====== * [[http://sourceforge.net/project/showfiles.php?group_id=38962&package_id=31152&release_id=352845|spice source]] * http://www-ti.informatik.uni-tuebingen.de/~bernauer/lehre/ti-1-0506/spice/ngspice.pdf