Page 1 of 2

IIR sine wave help

Posted: Tue Feb 18, 2014 11:27 pm
by JLS
Hi all

Is possible help me with simple example how to generate 1bit pwm audio output with this IIR sine wave algo ?

Code: Select all


int main ( void )
{

   long A=0x7e66;
   long buf[3]={0,0x1209,0};
   int val;

    while(1)
    {

            buf[ 0 ] = ((A * buf[ 1 ]) >> 14 ) - buf[ 2 ];
            buf[ 2 ] = buf[ 1 ];
            buf[ 1 ] = buf[ 0 ];

            val = 128 + (buf[ 0 ] >> 8);

   }
}

Please help me with pwm audio output example.

Many thanks

Kamil

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 12:04 pm
by JLS
This is my example but not working produce error message - " warning: `c_pwm' not used in two parallel statements "

Code: Select all


#include <platform.h>
#include <xs1.h>
#include "pwm.h"
#include <xscope.h>

#define SAMPLE_RATE 44100

on stdcore[0]: port audio = XS1_PORT_1A;

long A=0x7e66;
long buf[3]={0,0x1209,0};
int val;


void xscope_user_init( void )
{
    xscope_register( 1
        ,XSCOPE_CONTINUOUS ,"signal" ,XSCOPE_INT ,"n");
}

int main ( void )
{

    chan c_pwm;

    while(1)
    {

            buf[ 0 ] = ((A * buf[ 1 ]) >> 14 ) - buf[ 2 ];
            buf[ 2 ] = buf[ 1 ];
            buf[ 1 ] = buf[ 0 ];

            val = 32768+buf[ 0 ];

            xscope_int(0, val);

            c_pwm <: val;

            pwm_server(c_pwm, audio, SAMPLE_RATE);

            delay_microseconds (10);

    }

    return 0;
}


Please help me many thanks

Kamil

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 12:28 pm
by Folknology
You need to use two threads (or logical cores) to seperate the pwm and sine tasks

Rewrite your main as a function sine and remove the pwn server :

Code: Select all

void sine (chanend pwm){
    while(1)
    {
            buf[ 0 ] = ((A * buf[ 1 ]) >> 14 ) - buf[ 2 ];
            buf[ 2 ] = buf[ 1 ];
            buf[ 1 ] = buf[ 0 ];

            val = 32768+buf[ 0 ];

            xscope_int(0, val);

            c_pwm <: val;

            delay_microseconds (10);
    }
}

Then create a new main with a par and your 2 tasks with a common channel between them :

Code: Select all

void main(void) {
  chan pwm;

  par {
    sine(pwm);
    pwm_server(pwm, audio, SAMPLE_RATE);
  }
}
I haven't checked the detail of your code..

regards
Al

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 1:12 pm
by JLS
Testing it and compile without errors but not working !

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 1:45 pm
by Folknology
Is xscope showing the correct waveform?

regards
Al

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 2:17 pm
by JLS
yes sine algo working on xscope

but pwm not produce sound (this is same pwm like SID projects)

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 2:21 pm
by Folknology
Maybe try adjusting the relative rates of the tasks your sine rate could be higher than the PWM sample rate.

regards
Al

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 2:45 pm
by JLS
compiling project OK when run this project generate this warning - "xrun: Program received signal ET_ILLEGAL_RESOURCE, Resource exception.
0x00010210 in pwm_server (c_samples=2147549698, p_pwm_output=66048, pwm_frequency=44100) at ../src/pwm.xc:33"

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 2:48 pm
by Folknology
Can you paste your code please using the code tag

regards
Al

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 2:49 pm
by infiniteimprobability
Looks like you are sending an int over but pwm is expecting a short..