damped sine wave.

332 views
Skip to first unread message

ramp...@gmail.com

unread,
Jun 13, 2013, 7:14:05 AM6/13/13
to mozzi...@googlegroups.com

 Hi.

   Thank you bery much for the library. it looks impresive!

   I have managed to include the midivox shield DAC (12 bit serial microchip MCP4921 ) to the library.

   i have forked on github if you are interested. (https://github.com/rampa069/Mozzi)

 Now i want to start building an analogue (DDS)  drumbox.

 I know i have samples. but i want to make it "virtual analogic" any idea on how to "damp" the sinewave?


 in my old code (generating the wave in ram in realtime) i have used this formula.

  sin(128*x)*exp(-2*x)

 but not sure on how to use it on mozzi.




Mr Sensorium

unread,
Jun 13, 2013, 11:18:32 AM6/13/13
to mozzi...@googlegroups.com
Hi,

great, I'm glad to hear about any forks and developments.

Would the Ead (Exponential Attack Decay) envelope generator do what you want? 
You can see it used in the envelopes>Ead_Envelope example.
It would probably sound better for drum sounds if it is used at audio rate. 

Let me know if you're looking for something else, or need a better explanation/example.

tim

ramp...@gmail.com

unread,
Jun 14, 2013, 6:44:31 AM6/14/13
to mozzi...@googlegroups.com


thanks.  i am getting som good results.

 in the example, i changed the noise to a sinewave and commented the setPhase line. now i get wath i want one of each 2 Beats :-O

 tried some values in the setPhase, but when i use this, the gain is not changed. any clue?

ramp...@gmail.com

unread,
Jun 14, 2013, 7:13:46 AM6/14/13
to mozzi...@googlegroups.com
 ok. changed the setPhase to 0 and it works. but not sure if the frequency is OK. it sounds very high to me.

 2 oscs 20+49Hz for a bassdrum sample sketch.


/*  Example playing an enveloped noise source
 *  using Mozzi sonification library.
 *
 *  Demonstrates Ead (exponential attack decay).
 *
 *  Circuit: Audio output on digital pin 9 (on a Uno or similar), or
 *  check the README or http://sensorium.github.com/Mozzi/
 *
 *  Mozzi help/discussion/announcements:
 *  https://groups.google.com/forum/#!forum/mozzi-users
 *
 *  Tim Barrass 2012.
 *  This example code is in the public domain.
 */

#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin1024_int8.h> // sine table for oscillator
#include <Ead.h> // exponential attack decay
#include <EventDelay.h>


#define CONTROL_RATE 256 // powers of 2 please

Oscil <SIN1024_NUM_CELLS, AUDIO_RATE> aSin(SIN1024_DATA);
Oscil <SIN1024_NUM_CELLS, AUDIO_RATE> bSin(SIN1024_DATA);
EventDelay <CONTROL_RATE>  kDelay; // for triggering envelope start
Ead kEnvelope(CONTROL_RATE); // resolution will be CONTROL_RATE

int gain;



void setup(){
  startMozzi(CONTROL_RATE);
  aSin.setFreq(49);
  bSin.setFreq(20);

  kDelay.start(1000);
}


void updateControl(){
  // jump around in audio noise table to disrupt obvious looping
  //aNoise.setPhase(rand((uint)WHITENOISE8192_NUM_CELLS));
  //aSin.setPhase(0);
  //bSin.setPhase(0);
 
  if(kDelay.ready()){
    aSin.setPhase(0);
    bSin.setPhase(0);
    unsigned int attack = 10;
    unsigned int decay = 400;
    unsigned int duration = attack +decay ;
    kEnvelope.start(attack,decay);
    kDelay.start(duration+100);
  }
  gain = (int) kEnvelope.next();
}


int updateAudio(){
  int out=aSin.next();
          +bSin.next();
         
  return (gain*out>>4);
}


void loop(){
  audioHook(); // required here
}


Mr Sensorium

unread,
Jun 14, 2013, 9:00:59 AM6/14/13
to mozzi...@googlegroups.com
Hi rampamac,
how about this.. the cast to long in updateAudio() is the main change.
...apart from the >>4 to >>8, but maybe you're using your own 12 bit dac code ... anyway it sounds and looks as expected in a spectrum plot here.. is it any different for you?
I'm guessing you're using setPhase so the sine waves both start at 0 on each beat, where they would have been constantly shifting before, as the oscillators are playing continuously.

#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin1024_int8.h> // sine table for oscillator
#include <Ead.h> // exponential attack decay
#include <EventDelay.h>


#define CONTROL_RATE 256 // powers of 2 please

Oscil <SIN1024_NUM_CELLS, AUDIO_RATE> aSin(SIN1024_DATA);
Oscil <SIN1024_NUM_CELLS, AUDIO_RATE> bSin(SIN1024_DATA);
EventDelay <CONTROL_RATE>  kDelay; // for triggering envelope start
Ead kEnvelope(CONTROL_RATE); // resolution will be CONTROL_RATE

byte gain;




void setup(){
  startMozzi
(CONTROL_RATE);
  aSin
.setFreq(49);
  bSin
.setFreq(20);

  kDelay
.start(1000);
}


void updateControl(){
 
// jump around in audio noise table to disrupt obvious looping
 
//aNoise.setPhase(rand((uint)WHITENOISE8192_NUM_CELLS));
 
//aSin.setPhase(0);
 
//bSin.setPhase(0);
 
 
if(kDelay.ready()){
    aSin
.setPhase(0);
    bSin
.setPhase(0);
   
unsigned int attack = 10;
   
unsigned int decay = 400;
   
unsigned int duration = attack +decay ;
    kEnvelope
.start(attack,decay);
    kDelay
.start(duration+100);
 
}

  gain
= kEnvelope.next();

}


int updateAudio(){
 
int out=aSin.next();
         
+bSin.next();

 
return ((long)out*gain)>>8;

ramp...@gmail.com

unread,
Jun 14, 2013, 2:35:58 PM6/14/13
to mozzi...@googlegroups.com


On Friday, 14 June 2013 15:00:59 UTC+2, Mr Sensorium wrote:


 better now. now. now another problem summing the wave.
 
 in my prevoous code (and in yours) in the updateAudio function, there was a semicolon in the addition to eliminate it. can you check wath happens if you sum the waves? in my side i only can get very low volume waves or distorted waves depending on the bit shifting. (in my case with 12 bit >>4 sounds distorted, >>5 sounds very low.
 

Mr Sensorium

unread,
Jun 15, 2013, 8:29:44 AM6/15/13
to mozzi...@googlegroups.com
Aha... sin1024_int8.h has reduced dynamic range - a left-over from something I should have removed -woops.  That explains the low levels.  But maybe it's also worth having a master level to adjust for particularities of the synthesis.

Here's another version ... it looks like an improvement could be to make a "drum" class, except classes introduce overhead in the audio calculations, reducing capability by about 10-20% in my experience.  That might be improved by having conditional audio updating for the class, depending on whether the envelope is below a threshold...just thoughts..

#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator

#include <Ead.h> // exponential attack decay
#include <EventDelay.h> // for metronome



#define CONTROL_RATE 256 // powers of 2 please


Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> bSin(SIN2048_DATA);
EventDelay <CONTROL_RATE>  kBeat; // for triggering envelope start
Ead kBassEnvelope(CONTROL_RATE); // resolution will be 1000ms/CONTROL_RATE

byte bass_env; // carries bass envelope signal from updateControl() to updateAudio()

void setup(){
  aSin
.setFreq(49);
  bSin
.setFreq(20);


 
unsigned int attack = 10;
 
unsigned int decay = 400;

  kBassEnvelope
.set(attack,decay);

  kBeat
.start(500); // beat rate approx 500ms ~ 120bpm
  startMozzi
(CONTROL_RATE);
}


boolean metro(){
 
if(kBeat.ready()){
    kBeat
.start();
   
return true;
 
}
 
else{
   
return false;
 
}
}


void bassStart(){
 
// align sine waves
  aSin
.setPhase(0);
  bSin
.setPhase(0);
 
// start envelope
  kBassEnvelope
.start();
}


void bassNext(){
  bass_env
= kBassEnvelope.next();
 
if(bass_env<20) bass_env = 0; // gate off grainy tail on rendered audio
}


void updateControl(){
 
if(metro()){
    bassStart
();
 
}
  bassNext
();
}


int updateAudio(){
 
// bass
 
int bass=aSin.next()+bSin.next(); // 8+8=9 bits
  bass
= ((long)bass*bass_env)>>8; // 9 bits
 
// master
 
byte master_volume = 215; // adjust for max level without overflow
 
// 9+8=17 bits, -2 bits = 15bits (but less because master_vol is less than full 8 bits)
 
int out = ((long)bass*master_volume)>>2; // for 14 bit output, note this would be >>4 for 12 bit output
 
return out;

Jean-Luc Deladrière

unread,
Nov 9, 2014, 3:30:45 AM11/9/14
to mozzi...@googlegroups.com
Hi 
I cannot compile this line
EventDelay <CONTROL_RATE>  kBeat; // for triggering envelope start
I have corrected  it to  :
EventDelay  kBeat; // for triggering envelope start
to get it work 
Is this ok or am I missing something ?

Tim Barrass

unread,
Nov 10, 2014, 7:53:06 PM11/10/14
to mozzi...@googlegroups.com
Hi,
your correction is correct!
An example for EventDelay is accessable in Arduino examples menu, File>Examples>Mozzi>02.Control>EventDelay.

--
You received this message because you are subscribed to the Google Groups "Mozzi-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozzi-users...@googlegroups.com.
To post to this group, send email to mozzi...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mozzi-users/0c2f188f-1e48-4de0-99c4-17fcef2e2784%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages