Sunday, July 19, 2015

pd48. The keyup object in Pure Data

The keyup object sends the ASCII of the key which is pressed and then released, to its outlet.


The ASCII of '0' is 48, '1' is 49, and so on.


First press Reset to clear float. Then press 3 numbers, such as 7, 9, 2. They will be properly routed by the demux and incrementing index to the 3 number boxes. Any further input will not be recorded, unless Reset is pressed again.


pd47. The demux object in Pure Data

The demux object takes index of outlet from the right inlet. Then whatever data is sent to left inlet is then sent to proper outlet. Because the index has to be sent first (default will be 0), the bang is connected to index message first and then the list message. You can make clear the order, visually speaking, by using trigger object.


The data that is sent to outlet 0 or outlet 1 is a list of 2 numbers. Here, the number of outlets is 2 since that is the number of arguments.


It is unpacked and sent to number boxes.


Friday, July 17, 2015

pd46. Discrete Fourier Transform in Pure Data

For the audio signal of last example, the Discrete Fourier Transform (DFT) is found using rfft~.


The amplitude of the DFT for a blocksize of 1024 is found. The default block size is 64. The amplitude is shown on right channel. Here for each 1024 samples or (1024/44100 seconds), we have Fourier Amplitude of the block.


Even thought the DFT takes only a block, theoretically the block is treated as one period in infinite wave. The left and right boundaries of a block will be different in general. Thus there is a discontinuity as each new period begins, and this affects the Fourier coefficients for high-frequency terms. For realistic analysis, you should window the audio signal with a hamming window, etc. These windows will ensure the two boundaries are near 0 or identically 0. However, this will require overlapping of audio. For 1 minute audio with overlapping of 2, you will have to analyze two minutes.


The left channel shows the input audio signal, which is the same as last example. The right channel show the amplitude of different frequencies.


pd45. Phase Modulation in Pure Data

Frequency modulation is an example of phase modulation, and which we covered earlier.


An osc~ object is the same as phasor~ object connected to a cos~ object. The phasor~ provides the phase, 0 to 1 in Pure Data (corresponding 0 to 2 pi in the real world).


Here, we use a sqrt~ object to change the phase. This leads to audio with a large frequency bandwidth. With phase modulation, it is possible to get complicated sounds with only a few changes.


By looking at the audio waveform we can see the low-frequency parts (slowly changing) and high-frequency parts (changing quickly). We could confirm by plotting Spectrum from Analysis menu in Audacity.


pd44. The samphold~ object in Pure Data

The samphold~ object samples the left inlet audio signal whenever the control signal on right inlet is decreasing.


A toggle switch, operating at 10 ms, forces sampling whenever it is undergoing 1->0 transition, and the sampled audio will go to the left channel.


The audio signal is a sinusoidal at 5-Hz, too low to be heard by humans. The audio objects throw~ and catch~ are used so are our connections are cleaner. The thrown audio forms the right channel.


The audio waveform shows the effect of the samphold~:


Thursday, July 16, 2015

pd43. List split and append objects in Pure Data

We can use a list of numbers to provide timed control signals to an audio generator or synth. Here, the audio generator is just osc~, but in general it might be anything.


We have a list of frequencies and delays. The freq is 110 Hz for first 50 ms, 220 Hz for next 50 ms, 330 Hz for next 50 ms, and 440 for the last 50 ms of this 200-ms audio.


The bang sends the entire list, and after each delay the list is smaller by 2. The first outlet of list split 2 will have the first 2 values of list, second (middle) outlet will have the remaining list. This is fed into list append, which is triggered by the delay. Since you are appending an empty list with the remaining list, the new list is the remaining list, which is again fed into list split 2. This will continue until all numbers are used.


The audio signal shows increasing frequencies after every 50 milliseconds:


Wednesday, July 15, 2015

pd42. The wrap~ object in Pure Data

The wrap~ object keeps the fractional part of audio. If the signal is 1.5, it will be wrapped to 0.5. Without wrapping, it would be clipped off.


The input audio signal from osc~ is multiplied by 2 so it is a 2*cos signal. The signal is thus between -2 and +2. The two clip~ objects pass either the positive part of the signal or negative part of the signal.


Each of the signal is wrapped. The positive is immediately wrapped. For the negative, we first adjust the sign so it is again positive. The wrap is done and then it is multiplied by -1, yielding back a negative signal. Finally, the positive and negative signals are summed. This summation is implied since two audio signals are flowing to only one audio channel.


The final audio signal is shown here.


pd41. The sqrt object in Pure Data

The sqrt~ object is used to shape an audio signal from an osc~ 440.


The sqrt~ results in the square root whenever number is positive, and 0, when it is negative (thus avoiding complex numbers).


Two square root objects are used, one when the signal is positive and one when the signal is negative. This results in a wave rich in harmonics.


This shows the affect of the the square root objects:


Tuesday, July 14, 2015

pd40. Reading a text file into an array in Pure Data

This is a text file which is in the same folder as the patch:


The message, when clicked, reads the text file and loads it into array1.


In the middle we have an array element of size 6 for visualization of array.


We can also use the tabread object and give at index. Here, we give the last index, 5. Pure Data use 0-based indexing.


pd39. The swap object in Pure Data

The swap object reverses the order of the inlets: left inlet goes to right outlet and right inlet goes to left outlet.


On the left side we show a swap object with two numbers on the inlets, which are reversed in the outlets. You have to connect the cold inlet (right inlet) first to the bang. The better method will be to have a trigger object, so you can tell order of connections just by looking.


On the right side, instead of using right inlet, we use 1, so 1 will always be on left outlet. Here, we are implementing 1/x, where x is the input into left inlet of swap 1.


pd38. The timer and pipe objects in Pure Data

A timer object times how long an interval occurs between a left inlet bang and right inlet bang.


A pipe object delays a message by some time, 100 milliseconds here. Thus the outlet of pipe will be active 100 millsecond later. The float is passed to a number box and also to bang, to convert float to bang.


Thus the second bang, input to right inlet of timer, is set by pipe and not meant to be set by user.


Monday, July 13, 2015

pd37. The zl object in Pure Data

The zl object is for manipulation of lists.


Here, we have a list of numbers. We can use zl slice index, to get two lists, one before index and one after. Thus zl slice 1 will get 1st value of list in outlet 1. zl ecils 1, will get last value in list in outlet 1. (ecils is slice spelled backwards). The are other zl objects, such as zl group size.


Here, we unpack a list of 4 numbers. You can see the left and right sides are similar. Instead of using numbers to display the values, we also use print objects. However, with numbers, we can see the values on the canvas rather than looking at main window. But this assumes the values are numbers, not symbols or lists.


Sunday, July 12, 2015

pd36. The freeverb~ object in Pure Data

Reverb of a signal can be obtained by using the freeverb~ object. It has defaults for how echoes are added in the simulation of a room.


Additionally, you can send messages, such as roomsize and damping. Here, we send it the wet message, using a control-rate line.


It is important to know the difference between audio rate line (line~) and control rate line (line). Control rate signals can change at a smaller rate than audio rate.


This shows the effect of the audio as wet is changed:


Saturday, July 11, 2015

pd35. Variable Delay in Pure Data

Similar to the last example, there are two audio inputs which are added together. The first is a sinusoidal at 440-Hz.


The second is also a sinusoidal, but delayed by a vd~ object. The vd~ is similar to the delread~ object but the delay may be variable.


The delay changes for 10.5 periods to 20.5 periods over 1 second. It should be noted that we have destructive interference at either end (0 amplitude), and there are 10 beats overall, first between 0 and 0.1s, second between 0.1 s and 0.2s, etc.



The first beat is shown here. If our signal was composed of many frequencies, this patch will resonate at different frequencies at different times.


Friday, July 10, 2015

pd34. Delay Lines in Pure Data

Time and frequency have reciprocal relationships. Thus a lot of audio effects depend on delays.


A delwrite~ object delays the incoming audio signal. Since it is audio rate, we have a tilde at the end. It has two arguments; the name of the delay line as well as its length (in milliseconds). The length has to be greater than any delays you might have later on. While you might want to make this very large, so to have any amount of delay, this will increase memory requirements. Thus, it should be a little more than the greatest delay required.


A delread~ object reads the delay line at the specified delay.


Here the input is a sinusoidal with amplitude of 0.5. The frequency of the sinusoid is 440 Hz, which corresponds to a period of 2.2727 milliseconds. A delay of 10 periods corresponds to 22.727 milliseconds, while a delay of 10.5 periods corresponds to a delay of 23.8636 milliseconds.


If a periodic signal is added with a delayed wave which is a multiple of period, there is constructive interference and leads to a wave double the amplitude, which is 0.5*2 = 1 in this case. If a periodic signal is added with a delay which is a multiple of the period plus half a period, destructive interference occurs and the new amplitude is 0.



The top shows destructive interference (it should be zero, however the amplitude is not totally zero because we approximated delay time to 4 decimal positions, etc.) The bottom shows constructive interference. If the signal had not been halved originally, there would have been clipping.


Thursday, July 9, 2015

pd33. The z object in Pure Data

The z~ object allows for delays of audio signal. The default delay for z~ is 1, corresponding to time of 1/44100 or about 22.7 microseconds.


If you want another amount of delay you have to write the number, such as z~ 2.


The left channel is a 3-point moving average of the sawtooth, which has the effect of low-pass filtering. The right channel is the result of the lop object on the sawtooth wave. The cutoff frequency for the low-pass filter is 1 kHz.



The moving average and lop filtering on sawtooth are shown here.


Wednesday, July 8, 2015

pd32. The clip object in Pure Data

Using the clip object we can retain signal within a selected range. Anything above max is clipped at max, and anything below min is clipped at min.


Here min = -0.5 and max = 0.5. The audio signal from an osc~ goes from -1 to +1. This signal will be clipped between half of that range.


The sinusoidal wave was only one frequency. But because of the distortion now we will have many frequencies present.


Now the waveform is more square-wave like:


The frequency spectrum (in Audacity, Effects menu and Plot Spectrum option) shows the importance of higher frequencies:


Tuesday, July 7, 2015

pd31. Playing back samples at different rates in Pure Data

We use the same sample as in the last example, except now only one channel is read. Also the output of soundfiler object, which is the number of samples, 19723 in this case, is used as the send variable samples.


Once the sound is loaded and samples calculated, we can adjust the vertical slider. You should change the slider properties so min is 0.5 and max = 2, or some similar range. This slider controls the duration. Thus 0.5 would correspond to playback at half the time or twice the frequency, that is, up one octave.


Whenever the slider is adjusted, the bang has to be selected so the line~ goes from 1 to samples (19723 here) in the new time and table is read with 4-point interpolation.


Monday, July 6, 2015

pd30. Sample based synthesis in Pure Data

One of the drum samples in LMMS DAW is used. It is an ogg file and should be converted to wav. You can convert it by using Audacity (open the ogg file and export it as wav). It has been copied to the sounds folder in current directory.


It is loaded into arrays d01_ch1 and d01_ch2, for the two channels. We first have to click on the read message to load the file and send it to the soundfiler object which will initialize the two arrays. For mono audio, there will be only one channel and one array. The resize option changes the arrays to size of sound, and which is 19723 samples in this case. Since 44100 samples is one second, 19723 samples is a little less than 0.5 second.


The tabplay will play the sample without interpolation, at the cd rate of 44100.


Sunday, July 5, 2015

pd29. Triangle Wave generation using Wavetable Synthesis in Pure Data

Similar to the last example, we will create a triangle wave. This time we store one period in a table.


The name of the table is triangle and we have a view of the table in the middle of canvas. It is of size 512, and the message of sinesum creates a wave composed of fundamental (f) , and partial 3f, and partial 5f. There are only odd frequencies in a triangle wave. We used these amplitudes: 0.5,-0.5/9 and 0.5/25 for the three sinusoids.


We use tabosc4~ to play the wave over and over with 4-point interpolation. A wave at a frequency of 440-Hz, can fit exactly 440 such waves in one second. One second corresponds to 44100 samples. Thus each period is 44100/440 samples, or about 100. The program pd will figure out the exact math and do any interpolations, since it is not exactly 100, but 100.227.


The mono audio shows the triangle waveform:


pd28. Additive Synthesis in Pure Data

Any periodic signal can be decomposed into Fourier components of sinusoidals at different frequencies. The different frequencies are multiples of the fundamental.


For the triangle wave, tri(f) = constant * (sin(f)-1/9*sin(3f)+1/25*sin(5f)+...). There is a common term of 2 pi t for each sine argument. However for pd that is not relevant. It will be if we are using Python or another language to create the waveforms.


A better approximation will result as we take more terms of the series. It is important to set the phases into the right inlet of osc~.


The mono audio signal shows it is now approximating a triangle wave.


pd27. High-pass filter in Pure Data

A high-pass filter can be implemented with the hip~ object.


There are two audio sources here, the noise object (white noise) and the pink object (pink noise).


The toggle switch controls which audio source is used. Initially we check the toggle so we hear filtered pink noise. Clicking on the bang, which activates writing as well, will turn off toggle and then turn it on 0.5 sec. later. This way 0.5 second is spend on each audio source.


The top of the graph shows the audio in Waveform view. The first 0.5 sec. are high-passed white noise, and the last 0.5 sec. are high-passed pink noise. From the spectrograph view, bottom, the more uniform distribution for pink noise portion can be seen.


Saturday, July 4, 2015

pd26. Sawtooth wave generator in Pure Data

The phasor object generates sawtooth wave at the frequency given, which is 440 Hz in the example.


The output of the phasor is adjusted by 0.5 to remove offset (dc component = 0.5).


Then it multiplied by 2 so the amplitude is 1. That is, it goes form -1 to +1.


The mono audio signal shows the sawtooth wave:


pd25. Frequency Modulation in Pure Data

In Frequency Modulation we want cos(carrier+beta*cos(mod)), where beta is the Modulating Index.


The modulating index can be greater than one since it argument into the cos function, corresponding to the angle.


Once we copy the saving portion (right), the savefile subpatch will be automatically copied.


The audio, in stereo, shows the result.


pd24. Streaming audio into Pure Data

The readsf~ object can read and stream an external audio file (such as wav or aiff).


First, a file must be opened. We use openpanel to open a GUI element to choose file on computer.


Once the file is loaded, you may press start, on the right side, to start audio.


Should you need to hear another sound, or even the same sound, you have to reopen the file first. Most small audio samples will be read into memory with other objects.


There is a print object. Often you will use print objects during debugging. Below the audio file is assumed to be stereo.


Friday, July 3, 2015

pd23. Reading and Writing Tables in Pure Data

A array of length 5 was created, and put on the left hand side, with the name of array1.


By clicking on the message in the upper right corner, we can set the five values of the array, as 0 to 0.4 in steps of 0.1.


Further, by clicking on the bang in the lower part of the canvas, we set a counter to read the table.


We use the modulo operation to make sure the number (corresponding to index) is always between 0 and 4. Alternatively, we can put 4 on the last inlet of the counter.


pd22. Bandpass filter in Pure Data

The bp object is a bandpass filter. We will again filter noise. White noise has a uniform frequency distribution. The bandpass filter will pass frequencies in a certain range (the bandwidth).


The bp object needs the central frequency in the bandpass region, as well as Q parameter defining the bandwidth, in a reciprocal relationship.


The savefile subpatch is employed, which was shown in last example.


There is a random object to change frequencies so the signal brightness seems to be changing in time. However it leads to clicks. Later, we will see how to avoid them.


The audio is shown on top and its spectrum is shown below. We note the clicks (denoted by vertical lines, and which are evident at times of 0.3 s and 0.7 s).


pd21. Subpatch to Save Wave File Audio in Pure Data

Since we will often want to save the audio output to wav file, it makes sense to write a subpatch for the saving process. This might be for analysis or for input to another application.


Writing a subpatch will save some space on main canvas and abstract some details. There will be three inlets, with two being numeric inputs to the subpatch. The subpatch which will be named savefile. The two numeric parameters are duration of file and number of file, such as in pd1.wav, pd2.wav, etc.


In the main canvas, we have to write pd savefile. Editing this object will bring a new canvas where we have the components shown below.


In the next few examples, we will shown how to use this patch. We do not have an outlet, since we have a send.


Thursday, July 2, 2015

pd20. Low-pass frequency object in Pure Data

The lop~ object is a low-pass filter.


We had earlier used a noise source (which has a uniform frequency distribution). Now the noise is filtered by a low-pass frequency with a cutoff frequency of 440 Hz.


Thus low-frequencies are passed, without much attenuation below 440 Hz, and with greater attenuation above 440 Hz.


The audio signal shows the effect of filtering:


Because of the low-pass filter, the frequency distribution is no longer uniform but decaying (Analysis menu in Audacity -> Frequency Spectrum):