History

slightly tweak new examples (thanks Alex)

Ge Wang committed 3 years ago

Commit cfcfba21 parent 7e5be402 files · +100 −82Browse files

Showing the changes to examples/basic (1 of 2 files). Show all

Unified · Split

examples/basic/pulse.ck+51−39
@@ -1,39 +1,51 @@
1//------------------------------------------------------------------------------1//------------------------------------------------------------------------------
2// Pulse Width Modulation2// name: pulse.ck
3// name: pulse.ck3// desc: basic demo using PulseOsc UGen to do pulse width modulation
4// desc: Basic demo of the PulseOsc UGen4//
5//5// This UGen defines an oscillator that uses Pulse Width Modulation (PWM), a
6// details:6// process that allows for the control of the width of output pulses while
7// This UGen defines an oscillator that uses Pulse Width Modulation (PWM), a7// keeping the frequency constant. The on/off ratio of the signal is called the
8// process that allows for the control of the width of output pulses while 8// "duty cycle". A pulse wave may be thought of as a square wave with adjustable
9// keeping the frequency constant. The on/off ratio of the signal is called the 9// time between cycles. A square wave has a duty cycle of 50% -- alternating
10// "duty cycle". A pulse wave may be thought of as a square wave with adjustable 10// between "on" and "off" in equal proportions. A duty cycle of 75% would mean
11// time between cycles. A square wave has a duty cycle of 50% -- alternating 11// the pulse signal is "on" three times as long as it is "off" in one cycle.
12// between "on" and "off" in equal proportions. A duty cycle of 75% would mean 12// The PulseOsc UGen can be used to create sound, or to control other signals!
13// the pulse signal is "on" three times as long as it is "off" in one cycle.13//
14// The PulseOsc UGen can be used to create sound, or to control other signals!14// author: Alex Han
15//15// date: Spring 2023
16// Alex Han, Spring 202316//------------------------------------------------------------------------------
17//------------------------------------------------------------------------------17
18 18// connect
19// Setting up19PulseOsc pulse => dac;
20PulseOsc pulse => dac;20// set gain
21 21pulse.gain( .2 );
22pulse.gain(.2);22// set frequency
23pulse.freq(440);23pulse.freq( 110 );
24 24
25// The key characteristic of PulseOsc is the ability to set the pulse width (i.e. duty cycle)25// The key characteristic of PulseOsc is the ability to set the pulse width
26// When the pulse wave is used to create sound, varying the duty cycle affects the timbre.26// (i.e. duty cycle). When the pulse wave is used to create sound, varying
27// This demo shows how the timbre changes significantly with small changes to the pulse width:27// the duty cycle affects the timbre. This demo shows how the timbre changes
28 28// significantly with small changes to the pulse width.
29while(true)29
30{30// time loop
31 for(int i; i < 20; i++)31while( true )
32 {32{
33 pulse.width(.05 * i); // .width() takes a float from 0.0 to 1.033 for( int i; i < 20; i++ )
34 chout <= "current duty cycle: " <= pulse.width();34 {
35 if(pulse.width() == .5) {chout <= " <- this is just a square wave!";}35 // .width() takes a float from 0.0 to 1.0
36 chout <= IO.newline();36 pulse.width( .05 * i );
37 1::second => now;37 // print
38 }38 chout <= "current duty cycle: " <= pulse.width();
39}39 // floating point equality
40 if( Math.equal( pulse.width(), .5) )
41 {chout <= " <- this is just a square wave!";}
42 // newline
43 chout <= IO.newline();
44 // next time step
45 .5::second => now;
46 }
47}
48
49// for more PWM fun...
50// 1) smoothly vary the pulse width (using a small time scale)
51// 2) filter the sound (e.g., with LPF)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t