Csound Csound-dev Csound-tekno Search About

[Csnd] Administrivia

Date2020-05-01 04:27
From"john,,,"
Subject[Csnd] Administrivia
Message for the May 2020


This is just a regular monthly reminder for the Csound list.

The Csound list welcomes posts from people with ALL levels of skill,
from the newest newbie to the most serious hacker or established
composer.  The subject and tenor of the posts varies dramatically
depending on what the current concerns are.  Newbies are sometimes
afraid to post because they read discussions about the
incomprehensible deep inner workings, and all they want to know is how
to get a sound to come out of their computer, or advice on how to get
mobile sounds.  Rest assured that your questions will be answered
quickly, and (usually) in a helpful and courteous manner.  We have all
been there.  Please post.

If you find a bug or want to request for a new feature for the csound
library please report it here and/or open a ticket by going to

    https://github.com/csound/csound/issues

Please note that front-ends and other csound-based systems (e.g blue,
CsoundQt, Cabbage) have their own reporting schemes.

The developers particularly love bug reports that say which platform,
operating system and version (we are currently maintaining 6.14 while
developing csound7) were involved and a simple test program.

If you are thinking of submitting new code please read the Submission
advice (Submitting_opcodes.md) and/or consult a core developer.

==List Administrator

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2020-05-12 01:49
FromDavid Bowen
Subject[Csnd] Bug in cpd2pch?
I was looking at the code for cps2pch as a model for a possible oct2pch opcode and noticed what I think is a bug in the handling of the frequency table option.The relevant code is:

int32_t cps2pch(CSOUND *csound, XENH *p)
{
    double  fract;
    double  loct;

    fract = modf((double)*p->pc, &loct);        /* Get octave */
    if (*p->et > 0) {
      fract = pow(2.0, loct + (100.0*fract)/((double)*p->et));
      *p->r = (MYFLT)(fract * 1.02197503906); /* Refer to base frequency */
    }
    else {
      MYFLT t = - *p->et;
      FUNC* ftp = csound->FTnp2Finde(csound, &t);
      int32_t len;
      if (UNLIKELY(ftp == NULL))
        return csound->PerfError(csound, &(p->h),Str("No tuning table %d"),
                                 -((int32_t)*p->et));
int32_t cps2pch(CSOUND *csound, XENH *p)
{
    double  fract;
    double  loct;

    fract = modf((double)*p->pc, &loct);        /* Get octave */
    if (*p->et > 0) {
      fract = pow(2.0, loct + (100.0*fract)/((double)*p->et));
      *p->r = (MYFLT)(fract * 1.02197503906); /* Refer to base frequency */
    }
    else {
      MYFLT t = - *p->et;
      FUNC* ftp = csound->FTnp2Finde(csound, &t);
      int32_t len;
      if (UNLIKELY(ftp == NULL))
        return csound->PerfError(csound, &(p->h),Str("No tuning table %d"),
                                 -((int32_t)*p->et));
      len = ftp->flen;
      while (fract>len) {
        fract -= len; loct++;
      }
      fract += 0.005;
      *p->r = (MYFLT)(1.02197503906 * *(ftp->ftable +(int32_t)(100.0*fract)) *
                      pow(2.0, loct));
    }

    /*  double ref = 261.62561 / pow(2.0, 8.0); */
    return OK;
}    }

    /*  double ref = 261.62561 / pow(2.0, 8.0); */
    return OK;
}


I believe the intent of the while loop is to handle the case where the pitch class is larger than the number of elements in the frequency table by incrementing the octave number and reducing the pitch class until the class is in the table range. However, fract is still a number between 0 and 1, so the while will always short circuit and then csound will access outside the table range. I think the corrected code should look something like this:

      len = ftp->flen;
      fract *= 100.0;
      while (fract>len) {
        fract -= len; loct++;
      }
      fract += 0.5;
      *p->r = (MYFLT)(1.02197503906 * *(ftp->ftable +(int32_t)fract) *
                      pow(2.0, loct));
    
David Bowen

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2020-05-12 17:44
Fromjohn
SubjectRe: [Csnd] Bug in cpd2pch?
Culd you provie an exampleinut that would fail?  I cannot see te prolem 
yet o please enlighten me.

==John ff

On Mon, 11 May 2020, David Bowen wrote:

> I was looking at the code for cps2pch as a model for a possible oct2pch opcode
> and noticed what I think is a bug in the handling of the frequency table
> option.The relevant code is:
> 
> int32_t cps2pch(CSOUND *csound, XENH *p)
> {
>     double  fract;
>     double  loct;
> 
>     fract = modf((double)*p->pc, &loct);        /* Get octave */
>     if (*p->et > 0) {
>       fract = pow(2.0, loct + (100.0*fract)/((double)*p->et));
>       *p->r = (MYFLT)(fract * 1.02197503906); /* Refer to base frequency */
>     }
>     else {
>       MYFLT t = - *p->et;
>       FUNC* ftp = csound->FTnp2Finde(csound, &t);
>       int32_t len;
>       if (UNLIKELY(ftp == NULL))
>         return csound->PerfError(csound, &(p->h),Str("No tuning table %d"),
>                                  -((int32_t)*p->et));
> int32_t cps2pch(CSOUND *csound, XENH *p)
> {
>     double  fract;
>     double  loct;
> 
>     fract = modf((double)*p->pc, &loct);        /* Get octave */
>     if (*p->et > 0) {
>       fract = pow(2.0, loct + (100.0*fract)/((double)*p->et));
>       *p->r = (MYFLT)(fract * 1.02197503906); /* Refer to base frequency */
>     }
>     else {
>       MYFLT t = - *p->et;
>       FUNC* ftp = csound->FTnp2Finde(csound, &t);
>       int32_t len;
>       if (UNLIKELY(ftp == NULL))
>         return csound->PerfError(csound, &(p->h),Str("No tuning table %d"),
>                                  -((int32_t)*p->et));
>       len = ftp->flen;
>       while (fract>len) {
>         fract -= len; loct++;
>       }
>       fract += 0.005;
>       *p->r = (MYFLT)(1.02197503906 * *(ftp->ftable +(int32_t)(100.0*fract)) *
>                       pow(2.0, loct));
>     }
> 
>     /*  double ref = 261.62561 / pow(2.0, 8.0); */
>     return OK;
> }    }
> 
>     /*  double ref = 261.62561 / pow(2.0, 8.0); */
>     return OK;
> }
> 
> I believe the intent of the while loop is to handle the case where the pitch
> class is larger than the number of elements in the frequency table by
> incrementing the octave number and reducing the pitch class until the class is
> in the table range. However, fract is still a number between 0 and 1, so the
> while will always short circuit and then csound will access outside the table
> range. I think the corrected code should look something like this:
> 
>       len = ftp->flen;
>       fract *= 100.0;
>       while (fract>len) {
>         fract -= len; loct++;
>       }
>       fract += 0.5;
>       *p->r = (MYFLT)(1.02197503906 * *(ftp->ftable +(int32_t)fract) *
>                       pow(2.0, loct));
>     
> David Bowen
> 
> Csound mailing list Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
> https://github.com/csound/csound/issues Discussions of bugs and features can
> be posted here
>

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2020-05-12 20:12
FromDavid Bowen
SubjectRe: [Csnd] Bug in cpd2pch?
John,

   I think this qualifies as an example:

<CsoundSynthesizer>
  <CsOptions>
    -odac
  </CsOptions>
  <CsInstruments>
    sr = 44100
    kr = 441
    nchnls = 1

         instr 1
    ipch cps2pch p4, p5
         print   ipch
         endin
  </CsInstruments>
  <CsScore>
    f 1 0 16 -2 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

    i 1 0 1 8.02 -1
    i 1 1 1 8.12 -1
    e 2
  </CsScore>
</CsoundSynthesizer>

The second print shows ipch = 0.00 whereas I would expect it to show ipch twice the value in the first print.

David Bowen

On Tue, May 12, 2020 at 11:44 AM john <jpff@codemist.co.uk> wrote:
Culd you provie an exampleinut that would fail?  I cannot see te prolem
yet o please enlighten me.

==John ff

On Mon, 11 May 2020, David Bowen wrote:

> I was looking at the code for cps2pch as a model for a possible oct2pch opcode
> and noticed what I think is a bug in the handling of the frequency table
> option.The relevant code is:
>
> int32_t cps2pch(CSOUND *csound, XENH *p)
> {
>     double  fract;
>     double  loct;
>
>     fract = modf((double)*p->pc, &loct);        /* Get octave */
>     if (*p->et > 0) {
>       fract = pow(2.0, loct + (100.0*fract)/((double)*p->et));
>       *p->r = (MYFLT)(fract * 1.02197503906); /* Refer to base frequency */
>     }
>     else {
>       MYFLT t = - *p->et;
>       FUNC* ftp = csound->FTnp2Finde(csound, &t);
>       int32_t len;
>       if (UNLIKELY(ftp == NULL))
>         return csound->PerfError(csound, &(p->h),Str("No tuning table %d"),
>                                  -((int32_t)*p->et));
> int32_t cps2pch(CSOUND *csound, XENH *p)
> {
>     double  fract;
>     double  loct;
>
>     fract = modf((double)*p->pc, &loct);        /* Get octave */
>     if (*p->et > 0) {
>       fract = pow(2.0, loct + (100.0*fract)/((double)*p->et));
>       *p->r = (MYFLT)(fract * 1.02197503906); /* Refer to base frequency */
>     }
>     else {
>       MYFLT t = - *p->et;
>       FUNC* ftp = csound->FTnp2Finde(csound, &t);
>       int32_t len;
>       if (UNLIKELY(ftp == NULL))
>         return csound->PerfError(csound, &(p->h),Str("No tuning table %d"),
>                                  -((int32_t)*p->et));
>       len = ftp->flen;
>       while (fract>len) {
>         fract -= len; loct++;
>       }
>       fract += 0.005;
>       *p->r = (MYFLT)(1.02197503906 * *(ftp->ftable +(int32_t)(100.0*fract)) *
>                       pow(2.0, loct));
>     }
>
>     /*  double ref = 261.62561 / pow(2.0, 8.0); */
>     return OK;
> }    }
>
>     /*  double ref = 261.62561 / pow(2.0, 8.0); */
>     return OK;
> }
>
> I believe the intent of the while loop is to handle the case where the pitch
> class is larger than the number of elements in the frequency table by
> incrementing the octave number and reducing the pitch class until the class is
> in the table range. However, fract is still a number between 0 and 1, so the
> while will always short circuit and then csound will access outside the table
> range. I think the corrected code should look something like this:
>
>       len = ftp->flen;
>       fract *= 100.0;
>       while (fract>len) {
>         fract -= len; loct++;
>       }
>       fract += 0.5;
>       *p->r = (MYFLT)(1.02197503906 * *(ftp->ftable +(int32_t)fract) *
>                       pow(2.0, loct));
>     
> David Bowen
>
> Csound mailing list Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
> https://github.com/csound/csound/issues Discussions of bugs and features can
> be posted here
>

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2020-05-12 20:18
FromPete Goodeve
SubjectRe: [Csnd] Bug in cpd2pch?
AttachmentsNone  

Date2020-05-12 20:37
FromDavid Bowen
SubjectRe: [Csnd] Bug in cpd2pch?
Pete,

   cpspch treats pch values like 8.15 as if it were 9.03. I'm assuming cps2pch is trying to duplicate that behavior. The simple multiply to get the index would work if we didn't allow this special case, but I believe the while loop is there to handle it, but the logic isn't quite right.

Dave Bowen

On Tue, May 12, 2020 at 2:18 PM Pete Goodeve <pete.goodeve@computer.org> wrote:
On Tue, May 12, 2020 at 05:44:08PM +0100, john wrote:
> Culd you provie an exampleinut that would fail?  I cannot see te prolem
> yet o please enlighten me.
>
I can't provide an example, but I looked at the code and I agree
with David.

Extracting:

    fract = modf((double)*p->pc, &loct);
    .........
      len = ftp->flen;
      while (fract>len) {
        fract -= len; loct++;
      }

'fract' is obtained by modf, so by definition is < 1, and is not
changed before the while loop.  'len' is the table length -- an
integer > 1.  So the loop can never be entered.

Actually, I can't decode the logic of that section myself.
Doesn't the fractional part always span an octave, as does
the table?  So shouldn't fract just be mutiplied by len
to get an index into the table?  What am I missing?

        -- Pete --

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2020-05-12 20:37
FromPete Goodeve
SubjectRe: [Csnd] Bug in cpd2pch?
AttachmentsNone  

Date2020-05-12 20:59
Fromjohn
SubjectRe: [Csnd] Bug in cpd2pch?
I would expect to see zero i te second print as the table i 16 long anf 
you look at the 12 value which is empty; i also note the same problem 
exists in cpsxpch

If I chage yor 16 fr 10 it works in my version.

Will commit soon.

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2020-05-12 21:13
FromPete Goodeve
SubjectRe: [Csnd] Bug in cpd2pch?
AttachmentsNone