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