C.14. Chapter 14

C.14.1. Exercises

C.14.1.1. Implement a beep sound similar to the stereo beep at the beginning, but with a variable frequency (see Listing C.38). Make the frequency change very slowly between 220.0 and 660.0 to achieve a siren effect. If you want to keep the source simple, don't do different things for the left and right channels.
C.14.1.2. Complete the missing cases in the StereoBalanceControl module above. See Listings C.39 and C.40 for the answers.
C.14.1.3. Rewrite the stereo beep example in a way that the beeps are spinning in circles from the left channel to the right channel and back to the left channel. Listing C.41 highlights the code to do this.

C.14.1.1. Implement a beep sound similar to the stereo beep at the beginning, but with a variable frequency (see Listing C.38). Make the frequency change very slowly between 220.0 and 660.0 to achieve a siren effect. If you want to keep the source simple, don't do different things for the left and right channels.

The trick is to use Synth_MUL and Synth_ADD to get the range right:


Example C.38. Implementing a Beep Sound with Variable Frequency

   1 
   2 // exercise1.cc
   3 
   4 #include "artsflow.h"
   5 #include "connect.h"
   6 
   7 using namespace Arts;
   8 
   9 
  10 int main()
  11 {
  12     Dispatcher dispatcher;
  13 
  14     Synth_FREQUENCY freq1,freqmod;   // object creation
  15     Synth_WAVE_SIN  sin1,sinmod;
  16     Synth_MUL       mulmod;
  17     Synth_ADD       addmod;
  18     Synth_PLAY      play;
  19 
  20     // the modulation frequency
  21     setValue(freqmod, 0.3);
  22     connect(freqmod, sinmod);
  23 
  24     // bring it from the range [-1..1] to [-220..220]
  25     setValue(mulmod,"invalue1",220.0);
  26     connect(sinmod,mulmod,"invalue2");
  27 
  28     // add 440, to achieve the desired range: [220..660]
  29     setValue(addmod,"invalue1",440.0);
  30     connect(mulmod,addmod,"invalue2");
  31 
  32     // and use it as input for the beep generation
  33     connect(addmod,freq1);
  34     connect(freq1, sin1);         // object connection
  35 
  36     connect(sin1, play, "invalue_left");
  37     connect(sin1, play, "invalue_right");
  38 
  39     // start and go ;-)
  40     freq1.start(); freqmod.start();
  41     sin1.start(); sinmod.start();
  42     addmod.start(); mulmod.start();
  43     play.start();
  44     dispatcher.run();
  45 }
  46 

C.14.1.2. Complete the missing cases in the StereoBalanceControl module above. See Listings C.39 and C.40 for the answers.

   1 
   2  case sbLeftOnly:
   3     for(i=0;i<samples;i++)
   4     {
   5         outleft[i] = inleft[i];
   6         outright[i] = inleft[i];
   7     }
   8     break;
   9 case sbRightOnly:
  10     for(i=0;i<samples;i++)
  11     {
  12         outleft[i] = inright[i];
  13         outright[i] = inright[i];
  14     }
  15     break;
  16 case sbReverse:
  17     for(i=0;i<samples;i++)
  18     {
  19         outleft[i] = inright[i];
  20         outright[i] = inleft[i];
  21     }
  22     break;
  23 


Example C.40. The Missing Cases in balance.cc

   1 
   2 if(strcmp(argv[1],leftonly) == 0)
   3     bcontrol.balance(sbLeftOnly);
   4 if(strcmp(argv[1],rightonly) == 0)
   5     bcontrol.balance(sbRightOnly);
   6 if(strcmp(argv[1],reverse) == 0)
   7     bcontrol.balance(sbReverse);
   8 

C.14.1.3. Rewrite the stereo beep example in a way that the beeps are spinning in circles from the left channel to the right channel and back to the left channel. Listing C.41 highlights the code to do this.

   1 
   2 // exercise3.cc
   3 
   4 #include "artsflow.h"
   5 #include "connect.h"
   6 
   7 using namespace Arts;
   8 
   9 
  10 int main()
  11 {
  12     Dispatcher dispatcher;
  13 
  14     Synth_FREQUENCY freq1,freq2,freqspin;   // object creation
  15     Synth_WAVE_SIN  sin1,sin2,sinspin;
  16     Synth_MUL       mulspin1,mulspin2;
  17     Synth_ADD       addspin1,addspin2;
  18     Synth_MUL       mul1spin1,mul1spin2,mul2spin1,mul2spin2;
  19     Synth_ADD       addleft,addright;
  20     Synth_PLAY      play;
  21 
  22     setValue(freq1, 440.0);       // set frequencies
  23     setValue(freq2, 880.0);
  24     setValue(freqspin, 0.4);
  25 
  26     connect(freq1, sin1);         // object connection
  27     connect(freq2, sin2);
  28     connect(freqspin, sinspin);
  29 
  30     // first side: (freqspin * 0.5) + 0.5  (is between 0..1)
  31     connect(sinspin, mulspin1, "invalue1");
  32     setValue(mulspin1, "invalue2", 0.5);
  33     connect(mulspin1, addspin1, "invalue1");
  34     setValue(addspin1, "invalue2",0.5);
  35 
  36     // first side: (freqspin * (-0.5)) + 0.5  (is between 1..0)
  37     connect(sinspin, mulspin2, "invalue1");
  38     setValue(mulspin2, "invalue2", -0.5);
  39     connect(mulspin2, addspin2, "invalue1");
  40     setValue(addspin2, "invalue2",0.5);
  41 
  42     // multiply sin1 with the (0..1) and (1..0) ranges
  43     connect(sin1,mul1spin1,invalue1");
  44     connect(addspin1,mul1spin1,"invalue2");
  45     connect(sin1,mul1spin2,"invalue1");
  46     connect(addspin2,mul1spin2,"invalue2");
  47 
  48     // multiply sin2 with the (0..1) and (1..0) ranges
  49     connect(sin2,mul2spin1,"invalue1");
  50     connect(addspin1,mul2spin1,"invalue2");
  51     connect(sin2,mul2spin2,"invalue1");
  52     connect(addspin2,mul2spin2,"invalue2");
  53 
  54     // left channel output
  55     connect(mul1spin1,addleft,"invalue1");
  56     connect(mul2spin2,addleft,"invalue2");
  57     connect(addleft, play, "invalue_left");
  58 
  59     // right channel output
  60     connect(mul2spin1,addright,"invalue1");
  61     connect(mul1spin2,addright,"invalue2");
  62     connect(addright, play, "invalue_right");
  63 
  64     // start and go ;-)
  65     freq1.start(); freq2.start(); freqspin.start();
  66     sin1.start(); sin2.start(); sinspin.start();
  67     mulspin1.start(); mulspin2.start();
  68     addspin1.start(); addspin2.start();
  69     mul1spin1.start(); mul1spin2.start();
  70     mul2spin1.start(); mul2spin2.start();
  71     addleft.start(); addright.start();
  72     play.start();
  73     dispatcher.run();
  74 }
  75 

C.14.2. Part IV

There are no exercises for the chapters in this part.