sound synthesis with pure datasgn14006/exercises/exercise3-2015.pdf · in pure data, where objects...

17
Sound synthesis with Pure Data 1. Start Pure Data from the programs menu in classroom TC307. You should get the following window: The DSP check box switches sound output on and off. Getting sound out First, we’ll set up a simple oscillator to test the sound. Open a new workspace by going to File>New. A blank window should pop up. To create an object in PD, go to Put>Object, or press ctrl+1.

Upload: others

Post on 18-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Sound synthesis with Pure Data

1. Start Pure Data from the programs menu in classroom TC307. You should get the following

window:

The DSP check box switches sound output on and off.

Getting sound out First, we’ll set up a simple oscillator to test the sound.

Open a new workspace by going to File>New. A blank window should pop up.

To create an object in PD, go to Put>Object, or press ctrl+1.

Page 2: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

A small box will appear. Click to place it on the workspace.

WE WILL NOW DEFINE WHAT TYPE OF OBECT THIS SHOULD BE.

Type osc~ into the box, then click outside of the box. You will see that the box is now and OSC or

oscillator object.

Create another object, except this time, set it as a dac~ in the same way as you set the OSC. This block

is the digital to analogue convertor, and defines what is sent to your computer soundcard.

Page 3: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Connect the output of the oscillator to the DAC. TO do this, click the box in the bottom left of OSC, when

a small circle appears, and hold and drag the resulting line to one of the top boxes on the DAC.

In general, connections on the bottom are outputs, and the top are inputs.

Repeat the process to connect the OSC output to the other DAC input. You should now have something

like this:

Now we have an oscillator connected to the soundcard output. However, the oscillator has no control

signals.

Therefore, we need to define the frequency that the oscillator will work at.

There are different types of objects in Pure Data. We need to drive the oscillator with a NUMBER input.

Go to Put>Number or press Ctrl+3, and place a number object above the oscillator. A box with a 0 will

appear. Connect this box to the LEFT input of the oscillator.

In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object

above, the left input is black. The object only updates when a signal is changed on the LEFT input.

Page 4: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

You should now have the following. You are ready to make some noise!

To edit the value in the number box, go to the Edit menu and take PD out of Edit Mode, or press Ctrl+E.

Page 5: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

You can vary the value in the oscillator driving number box. Click the box and type a sensible value, e.g.

440. Press return. This is the frequency in Hz at which the oscillator will produce a sine wave.

Now switch on Output from Pure Data. First, check that PC volume is set to a sensible level, then click

the DSP check box. Clicking the box next to the IN OUT meters will allow monitoring of the output level.

This is done in the MAIN PD window, now in the window where you are creating your objects.

Great, so we have sound output. Now to make some more interesting sounds. Turn the DSP tick box off.

Page 6: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

FM Synthesis in Pure Data As we know, FM synthesis creates spectrally complex sounds by modulating the frequency input of a

simple oscillator with another oscillator.

Draw a simple schematic for an FM synthesiser in the space below: (see lecture notes)

ANSWER:

What differences do you notice between the oscillator objects in Pure Data and those shown in the

‘typical’ diagram above?

ANSWER:

In order to control the amplitude (volume) of our sounds, we need to multiply by some gain factor.

Go back to our simple oscillator example. Go back INTO Edit Mode, and delete the connections between

our oscillator and the output. Do this by clicking on each connection, and pressing delete.

Page 7: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Add a multiplier object between the oscillator and the DAC by creating an object (ctrl+1) and typing *~

to set it.

We will use this multiplier to control the signal level going to the DAC. Also create a number object.

Connect them as shown:

You may notice that some of the connections are thick, and some thin. This is to do with the type of

signal they are carrying. Thin connections are ‘control’ signals, whilst thick connections carry audio

signals.

Page 8: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Turn the DSP tick box on. In the configuration shown above, there should be no sound coming out.

Why?

ANSWER:

Let’s add a label to make things clear. Goto Put>Comment and insert a comment saying ‘amplitude

control’ next to our latest number box.

Now, let’s vary the gain of the multiplier. Exit Edit Mode. HOLD SHIFT and upwards drag on the

amplitude control. Holding shift allows fine grain control of the numbers in number boxes.

Keep an eye on the audio level meters in the main window.

What happens if the amplitude control goes above 1 or below -1? What happens to the quality of the

outputted sound when this happens?

ANSWER:

Page 9: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

We saw how we could multiply two objects in pure data with a *~ object. We can also sum using a

+~operator.

In FM Synthesis, we use 2 frequencies; the carrier and modulator. Currently, we’re driving our output

oscillator with a single carrier frequency. TO get FM synthesis, we need to vary (modulate) the

frequency with which we’re driving the oscillator with.

Click and drag up and down on the carrier frequency control number as rapidly as you can, in order to

change the pitch up and down.

What do you hear? How would you describe the sound?

ANSWER:

We want to be able to vary the pitch more rapidly. Up to many hundreds of times per second. TO do this

we should use an oscillator.

Remember that the one way of writing the equation describing the output from an FM synthesiser is:

𝑥(𝑛) = 𝐴(𝑛)sin[2𝜋(𝑓𝑐 + 𝐼 sin(2𝜋𝑓𝑚𝑛)) 𝑛]

Page 10: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Which terms refer to the carrier signal? Which refer to the modulator?

ANSWER:

We need to sum the carrier frequency with a modulating frequency.

Set up the following:

By entering the terms in the boxes shown. Modulator Frequency and Modulator gain boxes are

numbers, whilst osc~and *~100 are objects.

Experiment with the effect of varying different parameters. Sometimes, due to the order of control

signals, varying one parameter will not have the expected effect until the ‘live’ input on an object is

varied.

What is the effect of varying the modulator frequency whilst the modulator gain is set to 0?

ANSWER:

Page 11: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Effect of modulation frequency: Set the carrier to a nice round number, E.G. 200 Hz.

Set modulator gain high E.g. 200, so the effects of the modulator are pronounced.

Set the modulator frequency to an integer multiple of the carrier. E.G. 200, 400 or 600Hz if the carrier is

200Hz.

How would you describe the sound?

ANSWER:

Now change the carrier frequency gradually so it is no longer an integer multiple. How does the sound

change?

ANSWER:

Now set the modulator frequency low, E.G 1 to 3 Hz. How would you describe the sound? Do you

perceive a complex timbre?

ANSWER:

Gradually increase the frequency, 1 or 2 Hz at a time. At some point, the quality of the sound should

change. At what modulation frequency does this happen, and how would you describe the change?

ANSWER:

Page 12: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Sound Synthesis with Matlab

Now we’ll try to model a desired instrument sound in Matlab using FM synthesis, filtering, and an ADSR

type envelope.

We will look at the frequencies present in the waveform and try to model these with FM, then apply a

gain envelope to model the dynamics.

Load the ‘oboeNote.wav’ file from the course webpage in Matlab. Samplerate is 16kHz. Use the

wavread function to load the note and store into a variable, along with the samplerate of the

audio.

Listen to the note.

soundsc(audioData, fs);

How does the QUALITY (timbre) of the sound change over time? Or does it remain fairly constant?

ANSWER:

Plot the note against time. If the audio file is 103201 samples long, how long is this in seconds?

ANSWER:

To plot against time, make a vector of the same length as the audio file (in samples), with

equally spaced numbers.

time=(1:length(audioData))./fs; %Vector mapping sample number to time

figure; plot(time, audioData);

xlabel ‘Time, seconds’;

Page 13: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

You should see something like the following plot:

Draw a rough envelope of the AMPLITUDE of the waveform over time:

ANSWER:

Now let’s look at the spectral content of the note, in order to inform parameters for our FM synthesis.

If the samplerate of the audio is 16kHz, what is the Nyquist frequency? (the highest frequency which

can be sampled)?

ANSWER:

Take the Fourier transform of the data.

AUDIODATA=fft(audioData);

When we take the FFT, of course we get complex valued data. We want to inspect the amplitude of the

contribution at each frequency.

figure; plot(abs(AUDIODATA));

Page 14: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

The figure produced has both the real frequencies and complex conjugates present. We want only the

first half of the values produced.

Create a new variable which has only the first N/2 samples present in AUDIODATA.

In much the same way as we plotted our time-domain signal against a vector containing time values, we

should plot our Fourier analysis against frequency, not sample number.

frequencies=((1:length(AUDIODATA))./length(AUDIODATA))*(fs/2);

figure; plot(frequencies, abs(AUDIODATA));

You should get the following plot, showing the magnitude of various frequency components:

Use the Datatip tool to inspect the following:

What is the frequency of the fundamental?

ANSWER:

What is the frequency of the next biggest peak?

ANSWER:

Page 15: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Fill in the following table, FOR THE 5 HIGHEST PEAKS (marked with dots):

Component Fundamental (f1)

Peak 2 Peak3 Peak 4 Peak5

Frequency

Multiple of f1 1

Difference from f1

0

Magnitude compared to f1

1

Page 16: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Now we should synthesis the 5 biggest frequency components using FM synthesis.

Load the script basicFM.m.

% ---- Test PM synthesis in Matlab ----- fs = 16000; % sample rate (Hz) fcarr = 1; % carrier freq (Hz) fmod = 1; % modulation freq (Hz) A = 1; % amplitude I = 2; % modulation index: ratio of the peak deviation of modulation to the

modulating freq n = 0:1/fs:1; % time vector in seconds % PM synthesize the sound y = A * sin( 2*pi*n.*fcarr + I*sin(2*pi*fmod*n)); % Check the spectrum Y = abs( fft(y))/length(y); plot( Y(1:round(length(Y)/2)), 'k'); title('Magnitude spectrum'); xlabel('frequency'); soundsc(y, fs); % Play the sound

Modify the values of fcarr (carrier frequency) and fmod (modulation frequency) so that they

correspond to the values you entered in the table above.

Run the script.

Inspect the frequency response of the sound produced. Is it similar to the oboe sound we are trying to

synthesise?

ANSWER:

Inspect the time-domain waveform. How does it vary from the time domain waveform of the oboe

sound we are trying to synthesise?

ANSWER:

Create a gain envelope for the synthesized waveform using the ‘linspace’ command, and concatenating

various linear portions together; E.G.

> env=[linspace(0, 1, 1000), linspace(1, 0, 2000)]

Creates a vector of values which increase from 0 to 1 over 1000 samples, then fall from 1 to 0 over 2000

samples.

Page 17: Sound synthesis with Pure Datasgn14006/exercises/Exercise3-2015.pdf · In Pure Data, where objects have 2 inputs, one of the inputs is ‘live’. You can see on the osc~ object above,

Make sure that your gain envelope is the same dimensions as the waveform to which it should be

applied, and multiply the two together.

How does the note sound? How does it compare to the target sound? How could it be improved?

ANSWER: