Suggestions on casting needed

Discussion in 'C++' started by Active8, Mar 13, 2005.

  1. Active8

    Active8 Guest

    Something's wrong and I think it's my pointer arithmetic. I'm
    reading from the soundcard, so the buffer is pointed to by char*
    Maybe the last time I did this it was with BYTE*

    For 16 bit stereo, the first short (signed word) is the first sample
    of the left channel, the second short is the first right channel
    sample.

    If I cast the pointer to this char buffer to short* will buf[0],
    buf[1], ... give me the first and second WORD samples, or will it be
    wrong? Here's one relevant snip I'm not sure of.

    for(int i=0;i<nsamps/2;i++)
    {
    if( ( (short*)buf ) > _ymax ) _ymax = ( (short*)buf );
    if( ( (short*)buf ) < _ymin ) _ymin = ( (short*)buf );
    }

    I suspect the fact that nsamps/2 in the for stmnt works and nsamps
    crashes means that the cast is not fixing up the pointer arithmetic.
    And I'd need i+=2 for each iteration of the loop. Even doing that
    gives the wrong result. _ymax ends up being greater than _ymin, but

    _yceiling = ceil(_ymax); // part of scaling calcs for a plot.
    _yfloor = floor(_ymin); // it worked for simple bufs with no casting

    gives me a ceiling less than the floor. All variables prefixed with
    _y are floats, so maybe there's a loss of data (sign?). I can't
    think it through.

    Maybe someone could suggest how they would deal with this.

    Thanks in advance.
    --
    Best Regards,
    Mike
    Active8, Mar 13, 2005
    #1
    1. Advertising

  2. Active8

    David White Guest

    "Active8" <> wrote in message
    news:vyhmsuttylf9$...
    > Something's wrong and I think it's my pointer arithmetic. I'm
    > reading from the soundcard, so the buffer is pointed to by char*
    > Maybe the last time I did this it was with BYTE*
    >
    > For 16 bit stereo, the first short (signed word) is the first sample
    > of the left channel, the second short is the first right channel
    > sample.
    >
    > If I cast the pointer to this char buffer to short* will buf[0],
    > buf[1], ... give me the first and second WORD samples, or will it be
    > wrong? Here's one relevant snip I'm not sure of.


    Yes, it will give the 16-bit samples if the indexing comes after the cast.

    > for(int i=0;i<nsamps/2;i++)
    > {
    > if( ( (short*)buf ) > _ymax ) _ymax = ( (short*)buf );
    > if( ( (short*)buf ) < _ymin ) _ymin = ( (short*)buf );


    Just remove the /2 from the for loop - assuming that nsamps is the total
    number of 16-bit samples. Alternatively, you could cast buf to a short* at
    the outset and store it in a short* variable and use that in the loop
    without a cast.

    DW
    David White, Mar 13, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MattB
    Replies:
    0
    Views:
    418
    MattB
    Feb 7, 2005
  2. Ganesh
    Replies:
    2
    Views:
    2,411
    Chris Uppal
    Apr 3, 2004
  3. kevin
    Replies:
    11
    Views:
    5,785
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    511
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    551
    James Kanze
    Dec 20, 2009
Loading...

Share This Page