Cornell University ECE4760

Digital Sound Synthesis

PIC32MX250F128B

** Direct Digital Synthesis**, **FM, Physical synthesis**

**FM sound synthesis**

FM synthesis can be used to make a more interesting sound that can be string-like, or drum-like, or just plain strange.

The basic waveform equation is:

`wave = envelope`

_{main}* sin(F_{main}*t + envelope_{fm}*(sin(F_{fm}*t)))

For each of the envelopes, I used exponential functions, such that:

`envelope = A`

_{o}* e^{(-dk_rate*t)}* (1-e^{(-attack_rate*t)})

These exponential functions are compactly calculated at each time step as first-order differential equations, requiring one multiply each, if the decay and attack rates are specified as the fractional change in amplitude per decay sample. So a slow rate of decay would be 0.999 and a fast rate 0.005. A fast rise would be 0.0 and a slow rise 0.99. This is based on a Euler approximation of the first order differential equation:

`(y(n) - y(n-1))/dt = -K*y(n-1)`

with`y(0)=Ao`

and`K`

the decay rate

Re-arranging gives

`y(n)/y(n-1) = 1-K*dt = R`

with`0≤R<1 (`

because`K*dt<1`

for Euler approximation), so we can just compute`y(n)=y(n-1)*R`

Note that for significant amplitude, you need the attack time to be shorter than the decay time, unless you add some 'sustain' time, which holds the amplitude at full output level for a set time, then lets it decay.

Example:

All arithmetic is done in 16:16 fixed point for good accuracy and speed. The 32-bit fixed notation gives enough dynamic range so that the algorithm does not dither in the range of the DAC. The macros for fixed point are given on a separate page and in the code below. The oscillators are implemented as Direct Digital Synthesis units, accessing a 256 entry sine table. The output is to one channel of a stereo 12-bit SPI attached DAC. (code, ZIP of project). About 5-10% of the cpu is used to generate one FM synth channel, depending on which ISR operations are executing (at opt level 1). This was measured by toggling an i/o pin in the ISR. A separate program just outputs to damped sine waves to the stereo DAC channels. All programs use Protothreads to control execution.

example F_main F_fm Attack_main Attack_fm DK_main DK_fm depth_fm Sustain time130.0010.0010.980.802.20.00.50.80.0010.0010.990.901.50.0.250.370.0050.0050.980.982.00.02617830.990.950.980.973.01.0

A FM version with 8 predefined timbres plays a pentatonic scale. There is a simple command structure that lets you set:- timbre 0-7;
- tempo 0-3 ; 8/sec to 1/sec
- play a note; 0-7; C4, D4, E4, G4, A4, C5, D5, E5
- play a scale

**Karplus-Strong Algorithm (KSA) sound synthesis**

The Karplus-Strong Algorithm (KSA) for plucked string synthesis is a special case of the digital waveguide technique for musical instrument synthesis. The KSA is particularly well suited for implementation on a microcontroller, and was originally programmed on an Intel 8080! The simplest version hardly needs fixed point arithmetic, but better approximations do require some more sophisticated digital filters. The basic algorithm fills a circular buffer (delay line) with white noise, then lowpass filters the shifted output of the buffer and adds it back into itself. The length of the buffer (along with an allpass fractional tuning filter and the sample rate) determine the fundamental of the simulated string. The type of lowpass filter in the feedback determines the timber. Prefiltering the white noise before loading the buffer changes the pluck transient timber.Driving the Karplus-Strong string with a synthesized waveform expands the range of sounds you can make. In this version of the program, the string can be plucked using an initial waveform consisting of noise, sawtooth, triangle or sine wave. It can also be driven by a rising and falling amplitude waveform of all the previous types. In the code, each sample takes around 360 cycles to compute. The KSA output is sent to a SPI-attached DAC. The string length of 76.45 (at 20KHz sample rate) corresponds to approximately note C4 (261.8 Hz). The examples below are generated from the C code. In the table below, the

*noise*examples are Gaussian-distributed white noise, and the*Gaussian*examples are driven by a exp(-(pluck_position-string_index)^{2}/pluck_width) curve on the string. The bow_rise parameter is the 0 to 100% time in samples to the bow_amp value. The bow_fall parameter is the 100 to 0% time in samples from the bow_amp value to zero amplitude. The*wave*parameters describe the waveform which is pre-loaded as an initial condition, or driven into the model as a function of time.

(code, zip)example lowpass damping pluck_amp pluck_wave bow_amp bow_wave bow_rise bow_fall10.9991white noise0.--.----10.991Gaussian pulse0.------10.990--0.1white noise1000010010.950.--0.1Gaussian pulse10000100

**Finite-Difference, Time-Domain modeling of 1D system**

Solving the wave equation directly (*Study Notes on Numerical Solutions of the Wave Equation with the Finite Difference Method*, equation 2.15) yields the finite difference form to step the solution forward one sample.

Where u

DMA memcpy actually slows down the computation, propably because the vectors are too short to compensate for the setup time.^{n+1}is the displacement at the new time step and i represents the distance along the string. ρ is the propagation speed (range 0 to 1.0 for Courant stability), η is the dissipation which is small (typically 0.001 or so). The actual form of the equation in the code has been modified for efficiency. Most of the multplies have been replaced by shift-add operations. Because η is small, we can Taylor expand and show that

The**actual code**defines multiply macros using shift and add:

#define times0pt5(a) ((a)>>1)

#define times0pt25(a) ((a)>>2)

#define times2pt0(a) ((a)<<1)

#define times4pt0(a) ((a)<<2)

#define times0pt9998(a) ((a)-((a)>>12))

#define times0pt999(a) ((a)-((a)>>10))

Then uses these macros for the computation. The only full multiplication is by ρ to allow setting the pitch of the string.

for (i=1; i<string_size-1; i++){

new_string_temp = rho * (string_n[i-1] + string_n[i+1] - times2pt0(string_n[i]));

new_string[i] = times0pt9998(new_string_temp + times2pt0(string_n[i]) - times0pt9998(string_n_1[i])) ;

}

memcpy(string_n_1, string_n, copy_size);

memcpy(string_n, new_string, copy_size);

**References**

*A Tutorial on Digital Sound Synthesis Techniques*, Giovanni de Poli,: Computer Music Journal, Vol. 7, No. 4 (Winter, 1983), pp. 8-26- The Synthesis of Complex Audio Spectra by Means of Frequency Modulation, John Chowning,

Journal of the Audio Engineering Society, September 1973 - Physical modeling using digital waveguides

Julius O. Smith, III

Computer Music Journal, Vol. 16, No. 4. (Winter, 1992), pp. 74-91. - Digital Synthesis of Plucked-String and Drum Timbres

Kevin Karplus; Alex Strong

Computer Music Journal, Vol. 7, No. 2. (Summer, 1983), pp. 43-55. - Extensions of the Karplus-Strong Plucked-String Algorithm,

David A. Jaffe; Julius O. Smith

Computer Music Journal, Vol. 7, No. 2. (Summer, 1983), pp. 56-69. - Theory of Banded Waveguides

Essl, Georg, Serafin, Stefania, Cook, Perry R.,Smith, Julius O. (Julius Orion)

Computer Music Journal, Volume 28, Number 1, Spring 2004 - Musical Applications of Banded Waveguides

Essl, Georg, Serafin, Stefania, Cook, Perry R.,Smith, Julius O. (Julius Orion)

Computer Music Journal, Volume 28, Number 1, Spring 2004 - A review of musical synthesis techniques from 2006 is
*Discrete-time modelling of musical instruments,*by Valimaki V, Pakarinen J, Erkut C, Karjalainen M, in REPORTS ON PROGRESS IN PHYSICS 69 (1): 1-78 JAN 2006 - http://web.eecs.utk.edu/~qi/ece505/project/proj1.pdf
- https://ccrma.stanford.edu/~sdill/220A-project/drums.html#fm

Copyright Cornell University November 6, 2018