Csound Csound-dev Csound-tekno Search About

Re: Pulse width modulation!

Date1998-12-13 15:10
FromJosep M Comajuncosas
SubjectRe: Pulse width modulation!
> I found your old message where you posted an example orc. Your approach
> does sound a lot nicer on the higher notes. *BUT*...
>
> --it requires sr=kr which I frequently don't like.

Yes. I always set sr=kr. I never use Csound for realtime.

> --First impression: it sounds subjectively weaker in the bass compared
> to my approach. Maybe that's fixable.

It might be due to the leaky integrator. Using a factor closer to 1 , or
even 1 (the integ opcode just do that) should fix the problem, at the cost
of major sensitivity to initial DC

> --it glitches badly below 50 hz, and I don't understand your approach
> well enough to fix that.

Really? Maybe I set the maximum delay time expected in delayr to 1/50 sec,
simply put a bigger buffer of say 1 sec...

>
>
> Today I spent some time playing with another approach:

> I'll post it when I've got it  cleaned up and tweaked a bit better.

Yes please, I´d be very interested in it!

Josep M Comajuncosas




Received: from shaun.maths.bath.ac.uk by omphalos.maths.Bath.AC.UK id aa21358;
          13 Dec 98 18:28 GMT
Received: from [144.173.6.14] (helo=exeter.ac.uk)
	by shaun.maths.bath.ac.uk with esmtp (Exim 1.92 #2)
	for jpff@maths.bath.ac.uk
	id 0zpGFg-00048h-00; Sun, 13 Dec 1998 18:28:20 +0000
Received: from noether [144.173.8.10] by hermes via SMTP (SAA19709); Sun, 13 Dec 1998 18:25:57 GMT
Received: from exeter.ac.uk by maths.ex.ac.uk; Sun, 13 Dec 1998 18:25:41 GMT
Received: from howl.werewolf.net [206.103.224.20] by hermes via ESMTP (SAA14387); Sun, 13 Dec 1998 18:25:39 GMT
Received: from default (dial95.werewolf.net [206.103.225.105])
	by howl.werewolf.net (8.9.0/8.9.0) with SMTP id MAA05220
	for ; Sun, 13 Dec 1998 12:23:26 -0600 (CST)
Message-ID: <000b01be26c6$8fa52ee0$69e167ce@default>
From: Hans Mikelson 
To: Csound mailing list 
Subject: Re: Pulse width modulation!
Date: Sun, 13 Dec 1998 12:29:45 -0600
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Sender: owner-csound-outgoing@maths.ex.ac.uk
Precedence: bulk

Hi,

I'm planning on implementing this as a vco opcode so if someone comes up
with something better than what I have now I'll use that instead if they
don't mind.

Bye,
Hans Mikelson



Received: from shaun.maths.bath.ac.uk by omphalos.maths.Bath.AC.UK id aa21548;
          13 Dec 98 21:46 GMT
Received: from [144.173.6.14] (helo=exeter.ac.uk)
	by shaun.maths.bath.ac.uk with esmtp (Exim 1.92 #2)
	for jpff@maths.bath.ac.uk
	id 0zpJLi-0004Gr-00; Sun, 13 Dec 1998 21:46:46 +0000
Received: from noether [144.173.8.10] by hermes via SMTP (VAA25471); Sun, 13 Dec 1998 21:44:09 GMT
Received: from exeter.ac.uk by maths.ex.ac.uk; Sun, 13 Dec 1998 21:43:56 GMT
Received: from mail2.adinet.com.uy [206.99.44.33] by hermes via ESMTP (VAA12308); Sun, 13 Dec 1998 21:43:53 GMT
Received: from luis-jure (r40-17.uruguay.net.uy [200.2.40.17])
	by mail2.adinet.com.uy (8.9.0/8.9.0) with ESMTP id SAA08528
	for ; Sun, 13 Dec 1998 18:43:47 -0300 (GMT)
Message-Id: <199812132143.SAA08528@mail2.adinet.com.uy>
From: luis jure 
To: CSound list 
Subject: RV: Pulse width modulation!
Date: Sun, 13 Dec 1998 18:45:59 -0300
X-MSMail-Priority: Normal
X-Priority: 3
X-Mailer: Microsoft Internet Mail 4.70.1155
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by exeter.ac.uk id VAA12308
Sender: owner-csound-outgoing@maths.ex.ac.uk
Precedence: bulk

----------
> De: Paul Winkler 

> Erik Spjut has kindly reminded me that there have, in fact, been several
> PWM instruments posted to this list: his own, Josep M Comajuncosas', and
> Luis Jure's. I spent some time today playing with all three. They all
> have strong points and weak points. [...]
 
Well, I guess mine has mostly weak points. The idea was repeating a
linseg cyclically using reinit (kr=sr), and gradually change its form
with an envelope. The instrument was flexible in that the same
principle could be used for PWM or to modulate from sawtooth to
triangular, and you could use different envelopes for the modulation,
in one direction or the other, back and forth, etc. The obvious problem
was that the output was not bandlimited, and those straight lines and
angles waveforms have infinite number of harmonics, producing thus
heavy aliasing. Josep's design was much better, using buzz for the
band-limited pulse and a comb filter with variable delay for the
modulation. I don't have Erik's instrument, maybe you could post it
again to the list?

Not long ago I tried to implement in Csound the principles described in
an article about this topic:

John Lane, Dan Hoory, Ed Martínez, and Patty Wang (1997)
Modeling Analog Synthesis with DSPs.
Computer Music Journal, 21:4, pp. 23-41.

They use the absolute value of a sine wave and a highpass filter to
produce band-limited sawtooth, and in different combinations triangle
and square waves can also be obtained. It works; the exact settings of
the filters are critical, though. Some of the Csound experts with a good
knowledge of filter theory and DSP could develop this further.


Luis Jure






Received: from shaun.maths.bath.ac.uk by omphalos.maths.Bath.AC.UK id aa21792;
          14 Dec 98 1:27 GMT
Received: from [144.173.6.14] (helo=exeter.ac.uk)
	by shaun.maths.bath.ac.uk with esmtp (Exim 1.92 #2)
	for jpff@maths.bath.ac.uk
	id 0zpMnB-0004Q5-00; Mon, 14 Dec 1998 01:27:21 +0000
Received: from noether [144.173.8.10] by hermes via SMTP (BAA03514); Mon, 14 Dec 1998 01:22:34 GMT
Received: from exeter.ac.uk by maths.ex.ac.uk; Mon, 14 Dec 1998 01:22:21 GMT
Received: from kgallagh@tempest.ocis.temple.edu [155.247.166.120] by hermes via ESMTP (BAA16965); Mon, 14 Dec 1998 01:22:16 GMT
Received: from localhost (kgallagh@localhost)
	by tempest.ocis.temple.edu (8.8.8/8.8.8) with SMTP id UAA01739
	for ; Sun, 13 Dec 1998 20:22:16 -0500 (EST)
Date: Sun, 13 Dec 1998 20:22:16 -0500 (EST)
From: Kevin Gallagher 
X-Sender: kgallagh@tempest.ocis.temple.edu
To: Csound Discussion List 
Subject: DOS
Message-ID: 
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-csound-outgoing@maths.ex.ac.uk
Precedence: bulk

Does anyone know of a version of Csound that runs in DOS and supports
realtime audio output?  The versions for 286, 386 and 486 at the Leeds
site don't support realtime output.  RTSound runs in the DOS prompt within
Win95, but when I'm actually in DOS mode it doesn't.  I was just wondering
because I want to see how much cleaner I can get things running without
using Windows at all.  Thanks.

				Kevin Gallager, kgallagh@astro.temple.edu
				Web - http://astro.temple.edu/~kgallagh



Received: from shaun.maths.bath.ac.uk by omphalos.maths.Bath.AC.UK id aa22123;
          14 Dec 98 5:27 GMT
Received: from [144.173.6.14] (helo=exeter.ac.uk)
	by shaun.maths.bath.ac.uk with esmtp (Exim 1.92 #2)
	for jpff@maths.bath.ac.uk
	id 0zpQXw-0004Zc-00; Mon, 14 Dec 1998 05:27:52 +0000
Received: from noether [144.173.8.10] by hermes via SMTP (FAA02250); Mon, 14 Dec 1998 05:22:59 GMT
Received: from exeter.ac.uk by maths.ex.ac.uk; Mon, 14 Dec 1998 05:22:46 GMT
Received: from servidor.unam.mx [132.248.10.5] (may be forged) by hermes via ESMTP (FAA23684); Mon, 14 Dec 1998 05:22:44 GMT
Received: from localhost (hpsilva@localhost)
	by servidor.unam.mx (8.8.8/8.8.8) with SMTP id XAA00196
	for ; Sun, 13 Dec 1998 23:21:56 -0600 (CST)
Date: Sun, 13 Dec 1998 23:21:56 -0600 (CST)
From: "Pablo Silva-Escuela Nacional de Musica, UNAM" 
To: Csound mailing list 
Subject: Re: [ANNOUNCE] unofficial linux csound 3.49.3.3e available
In-Reply-To: 
Message-ID: 
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-csound-outgoing@maths.ex.ac.uk
Precedence: bulk


Hi:

I was just wondering whether this linux release of Csound would be able to
run on a Mac G3 running LinuxPPC, or whether the code would be
Intel-clone-specific??

Thanks

Pablo Silva




Received: from wallace.maths.bath.ac.uk by omphalos.maths.Bath.AC.UK id aa22212;
          14 Dec 98 6:33 GMT
Received: from [144.173.6.14] (helo=exeter.ac.uk)
	by wallace.maths.bath.ac.uk with esmtp (Exim 1.92 #2)
	for jpff@maths.bath.ac.uk
	id 0zpRYf-0002DX-00; Mon, 14 Dec 1998 06:32:41 +0000
Received: from noether [144.173.8.10] by hermes via SMTP (GAA12140); Mon, 14 Dec 1998 06:29:19 GMT
Received: from exeter.ac.uk by maths.ex.ac.uk; Mon, 14 Dec 1998 06:29:07 GMT
Received: from nicb@ax-nicb.axnet.it [194.184.60.149] by hermes via ESMTP (GAA22242); Mon, 14 Dec 1998 06:29:04 GMT
Received: (from nicb@localhost)
	by ax-nicb.axnet.it (8.8.8/8.8.8) id HAA29214;
	Mon, 14 Dec 1998 07:28:42 +0100
Date: Mon, 14 Dec 1998 07:28:34 +0100 (ROM )
From: Nicola Bernardini 
To: "Pablo Silva-Escuela Nacional de Musica, UNAM" 
cc: Csound mailing list 
Subject: Re: [ANNOUNCE] unofficial linux csound 3.49.3.3e available
In-Reply-To: 
Message-ID: 
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-csound-outgoing@maths.ex.ac.uk
Precedence: bulk

On Sun, 13 Dec 1998, Pablo Silva-Escuela Nacional de Musica, UNAM wrote:

[snip]
> I was just wondering whether this linux release of Csound would be able to
> run on a Mac G3 running LinuxPPC, or whether the code would be
> Intel-clone-specific??

I don't know of anybody running it on LinuxPPC - but there's always
a first time :-). Actually, I'd be glad to adjust the configure.in file
to allow compilation and linking on LinuxPPC if needed, and then we could
go into gorier details (MIDI, I/O, etc.) - except I don't have a G3.

If you would like to join the people working expressely on this version,
please subscribe to csound-unix-dev@ilogic.com.au (instructions may be
found at www.ilogic.com.au/csound).

Thank you

Nicola

------------------------------------------------------------------------
Nicola Bernardini
E-mail: nicb@axnet.it
 
Re graphics: A picture is worth 10K words -- but only those to describe
the picture.  Hardly any sets of 10K words can be adequately described
with pictures.



Received: from shaun.maths.bath.ac.uk by omphalos.maths.Bath.AC.UK id aa22430;
          14 Dec 98 9:54 GMT
Received: from [144.173.6.14] (helo=exeter.ac.uk)
	by shaun.maths.bath.ac.uk with esmtp (Exim 1.92 #2)
	for jpff@maths.bath.ac.uk
	id 0zpUho-0004pR-00; Mon, 14 Dec 1998 09:54:20 +0000
Received: from noether [144.173.8.10] by hermes via SMTP (JAA08108); Mon, 14 Dec 1998 09:47:17 GMT
Received: from exeter.ac.uk by maths.ex.ac.uk; Mon, 14 Dec 1998 09:47:04 GMT
Received: from smtp1.erols.com [207.172.3.234] by hermes via ESMTP (JAA03280); Mon, 14 Dec 1998 09:46:59 GMT
Received: from oemcomputer (209-122-225-126.s126.tnt1.nyw.erols.com [209.122.225.126])
	by smtp1.erols.com (8.8.8/8.8.5) with SMTP id EAA03848;
	Mon, 14 Dec 1998 04:46:02 -0500 (EST)
Message-ID: <3674DE85.30EC@erols.com>
Date: Mon, 14 Dec 1998 04:46:45 -0500
From: Paul Winkler 
Reply-To: zarmzarm@erols.com
X-Mailer: Mozilla 3.03 (Win95; I)
MIME-Version: 1.0
To: Hans Mikelson 
CC: Csound mailing list 
Subject: Re: Pulse width modulation!
References: <000b01be26c6$8fa52ee0$69e167ce@default>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Sender: owner-csound-outgoing@maths.ex.ac.uk
Precedence: bulk

Hans Mikelson wrote:
> I'm planning on implementing this as a vco opcode so if someone comes up
> with something better than what I have now I'll use that instead if they
> don't mind.


Well, I have not yet tried your version, so I'll leave it up to you to
decide whether my approach is better or worse! Feel free to use my code
(or anything vaguely based on it) if you like.

So, here's a version of pulse-width modulation I'm pretty happy
with. It sounds pretty nice to my ear, and it allows for much less
aliasing than my first attempt. The aliasing can be reduced as much as
you want, at the expense of some brightness. And this doesn't
require sr = kr, so it works nicely in realtime
situations.

Incidentally, this should also work as a strategy for
reduced-aliasing simulated square waves... just leave out the offset!

Thanks to the folks who chimed in... I love seeing these radically
different approaches to the same problem.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;copyright 1998 Paul M. Winkler, zarmzarm@erols.com
;****++++
;**** Last modified: Mon Dec 14 03:52:13 1998
;****----
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


	sr	=	44100
 	kr	=	2205
	ksmps	=	20
	nchnls	=	1

instr 5
; another try by PW
; We simulate a square wave by overdriving a table index, and simulate
; pwm by varying an offset to the index.
; Aliasing is reduced by making the amount of overdrive be 
; inversely proportional to pitch.

  ipitch    =    cpspch( p4)
  ianti_alias     = 10          ; recommended range is 4 to about 30
  isquareness     =    sr/(ipitch * ianti_alias) 
; Infinite isquareness yields a square wave. isquareness < 1 
; yields a sine.
  knarrowness     =    gkLFO *isquareness
  aindex    oscil      isquareness, ipitch, 1    ;  sine wave index.
  aindex    =          aindex + knarrowness
  aout      table      aindex, 5, 1, 0 ; f5 should be straight line +- 1
  aout      dcblock     aout
  kamp      adsr        .01, .02, .5, .2
  kamp      =           kamp*32000*gkamp
            out         aout*kamp
endin

instr 98                        ; global fades
gkamp expseg 0.005, p4, 1, (p3 - (p4 * 2)), 1, p4, 0.005
; p4 is rise AND decay time
print gkamp
endin

instr 99                        ; global LFO
klfo linseg p4, p3, p5          ; ramp from p4 to p5
gkLFO oscil 0.5, klfo, 1         ; use f1
gkLFO = gkLFO + 0.5          ; range from 0 to 1
endin

;;;;;;;;;;;;;;;;;;;;; END OF ORC ;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;; BEGIN SCO ;;;;;;;;;;;;;;;;;;;;;;;;;;;

t 0 120

; LFO wave shape ... it's weighted toward the top.
; I should figure out how to make the modulation respond
; better to a linear control signal! 
f1 0 512 7    1 128 .5 128 -1 128 .5 128 1

; straight line for instr 5
f5 0 512 -7   -1 512 1 

; Set up LFO and fades.
i98 0 32 1 
i99 0 32  .3 .6 

; bass_1, track 0, now at 0
i 5 0 0.5 6.00 
i 5 0.5 0.5 6.01 
i 5 1 0.5 6.02 
i 5 1.5 0.5 7.00 
i 5 2 0.5 6.00 
i 5 2.5 0.5 7.00 
i 5 3 0.5 6.01 
i 5 3.5 0.5 6.02 
i 5 4 0.5 6.05 
i 5 4.5 0.5 6.00 
i 5 5 0.5 5.09 
i 5 5.5 0.5 6.02 
i 5 6 0.5 6.05 
i 5 6.5 0.5 6.04 
i 5 7 0.5 6.03 
i 5 7.5 0.5 6.00 

; bass_1, track 0, now at 8
i 5 8 0.5 8 
i 5 8.5 0.5 8.01 
i 5 9 0.5 8.02 
i 5 9.5 0.5 9 
i 5 10 0.5 8 
i 5 10.5 0.5 9 
i 5 11 0.5 8.01 
i 5 11.5 0.5 8.02 
i 5 12 0.5 8.05 
i 5 12.5 0.5 8 
i 5 13 0.5 7.09 
i 5 13.5 0.5 8.02 
i 5 14 0.5 8.05 
i 5 14.5 0.5 8.04 
i 5 15 0.5 8.03 
i 5 15.5 0.5 8 

; bass_1, track 0, now at 16
i 5 16 0.5 5 
i 5 16.5 0.5 5.01 
i 5 17 0.5 5.02 
i 5 17.5 0.5 6 
i 5 18 0.5 5 
i 5 18.5 0.5 6 
i 5 19 0.5 5.01 
i 5 19.5 0.5 5.02 
i 5 20 0.5 5.05 
i 5 20.5 0.5 5 
i 5 21 0.5 4.09 
i 5 21.5 0.5 5.02 
i 5 22 0.5 5.05 
i 5 22.5 0.5 5.04 
i 5 23 0.5 5.03 
i 5 23.5 0.5 5 

; bass_1, track 0, now at 24
i 5 24 0.5 11 
i 5 24.5 0.5 11.01 
i 5 25 0.5 11.02 
i 5 25.5 0.5 12 
i 5 26 0.5 11 
i 5 26.5 0.5 12 
i 5 27 0.5 11.01 
i 5 27.5 0.5 11.02 
i 5 28 0.5 11.05 
i 5 28.5 0.5 11 
i 5 29 0.5 10.09 
i 5 29.5 0.5 11.02 
i 5 30 0.5 11.05 
i 5 30.5 0.5 11.04 
i 5 31 0.5 11.03 
i 5 31.5 0.5 11 

e

;;;;;;;;;;;;;;; END SCO ;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;; Just for the heck of it, here's the Pscore perl
script
;;;;;;;;;;;;;;;;;;;; that generated the score. Note that I took out
;;;;;;;;;;;;;;;;;;;; "use strict" from Pscore.pm to get this to work.

#!/usr/bin/perl

$orc = "/home/pw/Orcs+Scores/PW/pwm.orc";
$sco = "/home/pw/Orcs+Scores/PW/playing1.sco";
$output = "inst5_test.wav";

open(OUTFILE, ">$sco") || die "Can't open output score: $!"; 
select OUTFILE;

use Csound::Pscore;

# Let's make a pattern of 16th notes with these pitches.
@pch = qw(6.00 6.01 6.02  7.00
	  6.00 7.00 6.01  6.02
	  6.05 6.00 5.09  6.02
	  6.05 6.04 6.03  6.00
	  );
for ($i = 0; $i < 16; ++$i) {
    @Current[$i] = [ i, 2, $i * .5, .5, $pch[$i] ];
# Can't make the list with qw() or $i doesn't get interpolated!
}

store_chunk("bass_1");

print("t 0 120\n");

comment("sine for instr 1");
# print("f1 0 16384 10 1 \n");
print("f1 0 512 7    1 128 .5 128 -1 128 .5 128 1 ");

# comment("pseudo-square wave, not normalized, for instr 2");
# print("f2 0  512 -7  20000 250 20000 0 -20000 262 -20000 \n");
# comment("triangle wave for instr 3");
# print("f3 0 512 7   0 128 1 256 -1 128 0 \n"); 

comment("straight line for instr 5");
print("f5 0 512 -7   -1 512 1 \n");  


$start = 0;
for ($i = 5; $i <= 5; ++$i) { # cycle through some instruments.
                              # TEMPORARILY SET TO ONLY USE i5 !!
    load_chunk( "bass_1");
    pfields(1, "= $i"); # Select an instrument

    $Now[$Track] = $start;

    comment("Set up LFO and fades.");
    $dur = 32 ;
    print("i98 $start $dur 2 \n");
    print("i99 $start $dur  .3 .6 \n");
    
    # Here's where stuff actually happens.
    play;
    pfields(4, "+=2"); # change octave
    play;
    pfields(4, "-=3");
    play;
    pfields(4, "+=6");
    play;
    $start = $Now[$Track]
}

# PERF TIME!
close OUTFILE;
system("csound -m 4 -W -o $output $orc $sco");