473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Suggestions on casting needed

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 )[i] > _ymax ) _ymax = ( (short*)buf )[i];
if( ( (short*)buf )[i] < _ymin ) _ymin = ( (short*)buf )[i];
}

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
Jul 23 '05 #1
1 1126
"Active8" <re*********@ndbbm.net> wrote in message
news:vy***************@ID-222894.news.individual.net...
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 )[i] > _ymax ) _ymax = ( (short*)buf )[i];
if( ( (short*)buf )[i] < _ymin ) _ymin = ( (short*)buf )[i];


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

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andy Franks | last post by:
Hi All, This is driving me nuts, especially since I have this working for my main application with no problem. I suspect it might be due to Namespace conflicts, but I'm not positive. Any help...
17
by: Richard A. Lowe | last post by:
Hi, here are answers for some of the questions I suggested a short time ago for the m.p.d.l.c FAQ Jon Skeet is organizing. It was Jon's intention that these be 'honed' by the group before being...
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
1
by: Rob Reagan | last post by:
Can someone explain to me why I must explicitly cast UP a class heiarchy when using option explicit? For instance, I have a typed dataset (which is derived from the DataSet class) and I'm passing...
9
by: Brian | last post by:
Hello! What is the proper syntax for casting? For example, how do I change an Integer in to a String if the variable is called Joe1 and has 20 assigned to it. Thanks, Brian
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
23
by: PeterOut | last post by:
If I had code like this. unsigned short usLimit=10 int a, i; for (i=0; i<(int)usLimit; ++i) { a=(int)usLimit; }
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.