<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Audio Synthesis</title>
		<link>http://forums.leaflabs.com/topic.php?id=931</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:26:25 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=931" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Audio Synthesis"</title>
			<link>http://forums.leaflabs.com/topic.php?id=931#post-5770</link>
			<pubDate>Wed, 27 Jul 2011 09:12:27 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">5770@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Reading the libmaple code in timer.c, the frequency for PWM is set to approximately 1.1KHz (72MHz / 65,536)&#60;/p&#62;
&#60;p&#62;So each sample value must last at least 0.91millseconds.&#60;br /&#62;
If new values are put in using pwmWrite more frequently than this, the values will be ignored. That may cause some problems.&#60;/p&#62;
&#60;p&#62;Assuming you want more than 2 values to define the wave (Nyquist–Shannon and all that),&#60;br /&#62;
then the highest frequency the default PWM frequency allows is going to be lower than 1.1KHz/n, where n is 3 or more. This might not be what you want.&#60;/p&#62;
&#60;p&#62;To get higher frequencies, you need to set the PWM period to a duration which makes sense. Based on other sampled audio systems, 44KHz is an okay place to start.&#60;br /&#62;
The range of value that the timer's counter can reach in that time, up-counting at 72MHz, is about 1,636 which is a little over 10bit resolution.&#60;/p&#62;
&#60;p&#62;So, as Poslathian wrote, synch to the timer (using the timer interrupt) and put one value into each timer channel on each interrupt. &#60;/p&#62;
&#60;p&#62;I agree with Poslathian, at something like 44KHz, I think the STM32F might be able to calculate a value in software, especially if you are only aiming for a single tone. More likely true if you used fixed point for the calculation (see the threads to find that library).&#60;/p&#62;
&#60;p&#62;I assume the SerialUSB is just for debugging, and is not normally used (it would cripple the loop duration as it is likely to get locked to the USB 1mSec or slower transfer period)&#60;/p&#62;
&#60;p&#62;(full disclosure: I am not a member of LeafLabs staff.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "Audio Synthesis"</title>
			<link>http://forums.leaflabs.com/topic.php?id=931#post-5764</link>
			<pubDate>Wed, 27 Jul 2011 03:02:41 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">5764@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Andrew,&#60;br /&#62;
 great! we love audio work, in fact there is now an audioshield available made by an enterprising user ( &#60;a href=&#34;http://wiki.openmusiclabs.com/wiki/AudioCodecShield&#34; rel=&#34;nofollow&#34;&#62;http://wiki.openmusiclabs.com/wiki/AudioCodecShield&#60;/a&#62; ). &#60;/p&#62;
&#60;p&#62;When trying to coerce the system into doing things real time, I find it is easiest to use the timer interrupts - configured to trigger every AUDIO_RATE (like 50KHz or something). &#60;/p&#62;
&#60;p&#62;If your system is fairly simply, you can generate all your samples inside the interrupt - when the system crashes, you know youre out of cycles! What happens is you fail to produce the sample before the next interrupt comes around. &#60;/p&#62;
&#60;p&#62;A more complex alternative is to generate your audio in the main loop, and shove the samples in a buffer, then read them out from the buffer in the interrupt. This is handy if you have a non-uniform sample-generate time. This also allows you to try and drop samples gracefully if you need to. &#60;/p&#62;
&#60;p&#62;For an example of how to use timer interrupts to generate audio, check out my &#34;THX Deep Note&#34; demo for maple mini. its here:&#60;br /&#62;
&#60;a href=&#34;https://github.com/leaflabs/projects/blob/master/thx/thx_pwm/thx_pwm.pde&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/projects/blob/master/thx/thx_pwm/thx_pwm.pde&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;if you need help digesting it, just come on back here and ask away ;)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mouldi101 on "Audio Synthesis"</title>
			<link>http://forums.leaflabs.com/topic.php?id=931#post-5763</link>
			<pubDate>Tue, 26 Jul 2011 14:43:29 +0000</pubDate>
			<dc:creator>mouldi101</dc:creator>
			<guid isPermaLink="false">5763@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi all,&#60;/p&#62;
&#60;p&#62;I'm pretty (read: very) new to Maple, and thought I would start off with a little audio synthesis...&#60;/p&#62;
&#60;p&#62;Anyway, first project is simple wavetable oscillator, I've written code, I have it all programmed so it works, and I can change the frequency...&#60;/p&#62;
&#60;p&#62;But, there is a problem... Though I can get it to do this, the board doesn't seem to want to go audio rate, which from the documentation it should. The code is programmed at the moment so that at high frequencies I'm skipping out entries on the wave table.&#60;/p&#62;
&#60;p&#62;The code I'm running is as such, with the PWM output going to a Rugged Circuits Audiovox shield. The board is a rev.5.&#60;/p&#62;
&#60;p&#62;float sinTable[256] = {0,0.049067674,0.09801714,0.146730474,0.195090322,0.24298018,0.290284677,0.336889853,0.382683432,0.427555093,0.471396737,0.514102744,0.555570233,0.595699304,0.634393284,0.671558955,0.707106781,0.740951125,0.773010453,0.803207531,0.831469612,0.85772861,0.881921264,0.903989293,0.923879533,0.941544065,0.956940336,0.970031253,0.98078528,0.98917651,0.995184727,0.998795456,1,0.998795456,0.995184727,0.98917651,0.98078528,0.970031253,0.956940336,0.941544065,0.923879533,0.903989293,0.881921264,0.85772861,0.831469612,0.803207531,0.773010453,0.740951125,0.707106781,0.671558955,0.634393284,0.595699304,0.555570233,0.514102744,0.471396737,0.427555093,0.382683432,0.336889853,0.290284677,0.24298018,0.195090322,0.146730474,0.09801714,0.049067674,1.22465E-16,-0.049067674,-0.09801714,-0.146730474,-0.195090322,-0.24298018,-0.290284677,-0.336889853,-0.382683432,-0.427555093,-0.471396737,-0.514102744,-0.555570233,-0.595699304,-0.634393284,-0.671558955,-0.707106781,-0.740951125,-0.773010453,-0.803207531,-0.831469612,-0.85772861,-0.881921264,-0.903989293,-0.923879533,-0.941544065,-0.956940336,-0.970031253,-0.98078528,-0.98917651,-0.995184727,-0.998795456,-1,-0.998795456,-0.995184727,-0.98917651,-0.98078528,-0.970031253,-0.956940336,-0.941544065,-0.923879533,-0.903989293,-0.881921264,-0.85772861,-0.831469612,-0.803207531,-0.773010453,-0.740951125,-0.707106781,-0.671558955,-0.634393284,-0.595699304,-0.555570233,-0.514102744,-0.471396737,-0.427555093,-0.382683432,-0.336889853,-0.290284677,-0.24298018,-0.195090322,-0.146730474,-0.09801714,-0.049067674,-2.44929E-16,0.049067674,0.09801714,0.146730474,0.195090322,0.24298018,0.290284677,0.336889853,0.382683432,0.427555093,0.471396737,0.514102744,0.555570233,0.595699304,0.634393284,0.671558955,0.707106781,0.740951125,0.773010453,0.803207531,0.831469612,0.85772861,0.881921264,0.903989293,0.923879533,0.941544065,0.956940336,0.970031253,0.98078528,0.98917651,0.995184727,0.998795456,1,0.998795456,0.995184727,0.98917651,0.98078528,0.970031253,0.956940336,0.941544065,0.923879533,0.903989293,0.881921264,0.85772861,0.831469612,0.803207531,0.773010453,0.740951125,0.707106781,0.671558955,0.634393284,0.595699304,0.555570233,0.514102744,0.471396737,0.427555093,0.382683432,0.336889853,0.290284677,0.24298018,0.195090322,0.146730474,0.09801714,0.049067674,3.67394E-16,-0.049067674,-0.09801714,-0.146730474,-0.195090322,-0.24298018,-0.290284677,-0.336889853,-0.382683432,-0.427555093,-0.471396737,-0.514102744,-0.555570233,-0.595699304,-0.634393284,-0.671558955,-0.707106781,-0.740951125,-0.773010453,-0.803207531,-0.831469612,-0.85772861,-0.881921264,-0.903989293,-0.923879533,-0.941544065,-0.956940336,-0.970031253,-0.98078528,-0.98917651,-0.995184727,-0.998795456,-1,-0.998795456,-0.995184727,-0.98917651,-0.98078528,-0.970031253,-0.956940336,-0.941544065,-0.923879533,-0.903989293,-0.881921264,-0.85772861,-0.831469612,-0.803207531,-0.773010453,-0.740951125,-0.707106781,-0.671558955,-0.634393284,-0.595699304,-0.555570233,-0.514102744,-0.471396737,-0.427555093,-0.382683432,-0.336889853,-0.290284677,-0.24298018,-0.195090322,-0.146730474,-0.09801714,-0.049067674};&#60;br /&#62;
float cosTable[256] = {0,0.049067674,0.09801714,0.146730474,0.195090322,0.24298018,0.290284677,0.336889853,0.382683432,0.427555093,0.471396737,0.514102744,0.555570233,0.595699304,0.634393284,0.671558955,0.707106781,0.740951125,0.773010453,0.803207531,0.831469612,0.85772861,0.881921264,0.903989293,0.923879533,0.941544065,0.956940336,0.970031253,0.98078528,0.98917651,0.995184727,0.998795456,1,0.998795456,0.995184727,0.98917651,0.98078528,0.970031253,0.956940336,0.941544065,0.923879533,0.903989293,0.881921264,0.85772861,0.831469612,0.803207531,0.773010453,0.740951125,0.707106781,0.671558955,0.634393284,0.595699304,0.555570233,0.514102744,0.471396737,0.427555093,0.382683432,0.336889853,0.290284677,0.24298018,0.195090322,0.146730474,0.09801714,0.049067674,1.22465E-16,-0.049067674,-0.09801714,-0.146730474,-0.195090322,-0.24298018,-0.290284677,-0.336889853,-0.382683432,-0.427555093,-0.471396737,-0.514102744,-0.555570233,-0.595699304,-0.634393284,-0.671558955,-0.707106781,-0.740951125,-0.773010453,-0.803207531,-0.831469612,-0.85772861,-0.881921264,-0.903989293,-0.923879533,-0.941544065,-0.956940336,-0.970031253,-0.98078528,-0.98917651,-0.995184727,-0.998795456,-1,-0.998795456,-0.995184727,-0.98917651,-0.98078528,-0.970031253,-0.956940336,-0.941544065,-0.923879533,-0.903989293,-0.881921264,-0.85772861,-0.831469612,-0.803207531,-0.773010453,-0.740951125,-0.707106781,-0.671558955,-0.634393284,-0.595699304,-0.555570233,-0.514102744,-0.471396737,-0.427555093,-0.382683432,-0.336889853,-0.290284677,-0.24298018,-0.195090322,-0.146730474,-0.09801714,-0.049067674,-2.44929E-16,0.049067674,0.09801714,0.146730474,0.195090322,0.24298018,0.290284677,0.336889853,0.382683432,0.427555093,0.471396737,0.514102744,0.555570233,0.595699304,0.634393284,0.671558955,0.707106781,0.740951125,0.773010453,0.803207531,0.831469612,0.85772861,0.881921264,0.903989293,0.923879533,0.941544065,0.956940336,0.970031253,0.98078528,0.98917651,0.995184727,0.998795456,1,0.998795456,0.995184727,0.98917651,0.98078528,0.970031253,0.956940336,0.941544065,0.923879533,0.903989293,0.881921264,0.85772861,0.831469612,0.803207531,0.773010453,0.740951125,0.707106781,0.671558955,0.634393284,0.595699304,0.555570233,0.514102744,0.471396737,0.427555093,0.382683432,0.336889853,0.290284677,0.24298018,0.195090322,0.146730474,0.09801714,0.049067674,3.67394E-16,-0.049067674,-0.09801714,-0.146730474,-0.195090322,-0.24298018,-0.290284677,-0.336889853,-0.382683432,-0.427555093,-0.471396737,-0.514102744,-0.555570233,-0.595699304,-0.634393284,-0.671558955,-0.707106781,-0.740951125,-0.773010453,-0.803207531,-0.831469612,-0.85772861,-0.881921264,-0.903989293,-0.923879533,-0.941544065,-0.956940336,-0.970031253,-0.98078528,-0.98917651,-0.995184727,-0.998795456,-1,-0.998795456,-0.995184727,-0.98917651,-0.98078528,-0.970031253,-0.956940336,-0.941544065,-0.923879533,-0.903989293,-0.881921264,-0.85772861,-0.831469612,-0.803207531,-0.773010453,-0.740951125,-0.707106781,-0.671558955,-0.634393284,-0.595699304,-0.555570233,-0.514102744,-0.471396737,-0.427555093,-0.382683432,-0.336889853,-0.290284677,-0.24298018,-0.195090322,-0.146730474,-0.09801714,-0.049067674};&#60;/p&#62;
&#60;p&#62;float oscOutput;&#60;br /&#62;
float i = 0.0;&#60;br /&#62;
float noteFreq = 200;&#60;br /&#62;
int bits = 8;&#60;br /&#62;
float steps = float(pow(2,bits));&#60;br /&#62;
int halfpwmRes = 65536/2;&#60;br /&#62;
int waveTable = 1;&#60;/p&#62;
&#60;p&#62;void setup(){&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void loop(){&#60;br /&#62;
  noteFreq = (analogRead(15)+25)*2;&#60;br /&#62;
  if(noteFreq==0){&#60;br /&#62;
    noteFreq=1;&#60;br /&#62;
  }&#60;/p&#62;
&#60;p&#62;  //Cycles scanning oscillator from 0 to 'steps'&#60;br /&#62;
  if(i&#38;gt;=steps){&#60;br /&#62;
    while(i&#38;gt;=steps){&#60;br /&#62;
      i=i-steps;&#60;br /&#62;
    }&#60;br /&#62;
  }&#60;br /&#62;
  else{&#60;br /&#62;
    pwmWrite(9,(i*steps)-1);&#60;br /&#62;
//      i=i+((steps/noteFreq)*100);&#60;br /&#62;
        i=i+((0.00943016759776536*noteFreq)+0.528491620111732); //Allows stepping through wavetables at various speeds defined by potentiometer&#60;br /&#62;
  }&#60;/p&#62;
&#60;p&#62;  //Selects wavetable, also uses scanning oscillator to look up value in wavetable&#60;br /&#62;
  switch (waveTable){&#60;br /&#62;
    case 1:&#60;br /&#62;
      oscOutput=sinTable[int(i)];&#60;br /&#62;
      break;&#60;br /&#62;
    case 2:&#60;br /&#62;
      oscOutput=cosTable[int(i)];&#60;br /&#62;
      break;&#60;br /&#62;
    default:&#60;br /&#62;
      oscOutput=sinTable[int(i)];&#60;br /&#62;
    break;&#60;br /&#62;
  }&#60;/p&#62;
&#60;p&#62;  //Outputs using pwm, scaled to fit between 0 and PWM resolution&#60;br /&#62;
  pwmWrite(9,(int((oscOutput+1)*halfpwmRes)));&#60;/p&#62;
&#60;p&#62;  //Debug via serial&#60;br /&#62;
  SerialUSB.print(&#34;Frequency = &#34;);&#60;br /&#62;
  SerialUSB.print(noteFreq);&#60;br /&#62;
  SerialUSB.print(&#34;   ScanOscVal = &#34;);&#60;br /&#62;
  SerialUSB.print(i);&#60;br /&#62;
  SerialUSB.print(&#34;   Output + &#34;);&#60;br /&#62;
  SerialUSB.print((oscOutput+1)*halfpwmRes);&#60;br /&#62;
  SerialUSB.print(&#34;\r\n&#34;);&#60;/p&#62;
&#60;p&#62;Any ideas greatly appreciated!&#60;/p&#62;
&#60;p&#62;Andrew
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
