0

I want to record data from a microphone using alsa. This command:

int buf[4096];
memset(buf, 0, sizeof(buf));
snd_pcm_readi(capture_handle, buf, avail);

writes the microphone data into the integer buffer buf. ( I am not sure if the data that is written by ..._readi is even integer values, the documentation does'nt tell.)

But if I iterate through the buffer the numbers make no sense. As an example I get that buf[60] == -2,600,000,000 so its smaller than the minimum integer if integer is 32 bit. ( as a note this is not my code but I have to work on it). I want to get the binary values of this whole buffer array and make sense of the values and look up in which way they are saved into the buffer so I can recreate the soundwave with this data.

relot
  • 651
  • 1
  • 6
  • 18

3 Answers3

4

Better use an array of char to record raw data. But to answer your question:

#include <limits.h>
#include <stdio.h>

void print_binary(int value)
{
    unsigned mask = ~(~0u >> 1);  // set highest bit

    // iterate over all bits:
    for (size_t i = 0; i < sizeof(value) * CHAR_BIT; ++i) {
        putchar('0' + !!(value & mask));  // !! converts to bool 0 or 1
        mask >>= 1;  // shift to next lower bit
    }
}

int main(void)
{
    int x = 9;
    print_binary(x);
    putchar('\n');
}

Output:

00000000000000000000000000001001

Shorter:

#include <stdio.h>

void print_binary(int value)
{
    for (unsigned mask = ~(~0u >> 1); mask; mask >>= 1)
        putchar('0' + !!(value & mask));
}


PS: Just to clarify ~(~0u >> 1) (8 bits for simplicity):

~0u                  negate all bits                                    1111 1111
~0u >> 1         shift to right 1 bit and fill up with 0      0111 1111
~(~0u >> 1)    negate that                                         1000 0000

Swordfish
  • 12,971
  • 3
  • 21
  • 43
1

You could write your own conversion specifier for printing in binary with printf.

Here's a SO answer on how to do that: https://stackoverflow.com/a/112947

After you code that, you should be able to do:

printf("%b", 5); /* prints 101 */

EDIT:

The good point about this is you can then (if you code it, obviously, which btw is a little tedious) use modifiers:

printf("%'#b", 0x15);

The way I programmed that (I still haven't had the opportunity to test it though), it should print:

0b1'0101

EDIT 2:

That link above explains an old deprecated way of doing it. However, it is very similar to the new one, and from reading it, I could do an implementation using the new method.

I posted my implementation here: https://codereview.stackexchange.com/q/219994/200418

-1
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>

int main(void) 
{
  int32_t data[5]= {2,1,1,1,1}; /*Example array with small value for easy recognition of binary value */
  int i,array_size,bit_size,bit_print;

  array_size=sizeof(data)/sizeof(data[0]);

  for(i=0;i<array_size;i++)
  {
    for(bit_size=31; bit_size>=0; bit_size--)
    {
        bit_print = data[i] >> bit_size;

        if (bit_print & 1)
          printf("1");
        else
          printf("0");
    }
    printf("\n"); /*this new line is optional if you want to view each value on new line.*/
  }  
}
Nitin Jadhav
  • 497
  • 1
  • 7
  • 17
  • 1
    Right shift of negative signed integers is implementation-defined. – Swordfish May 08 '19 at 14:47
  • I am not clear with your statement. Do you mean considering the signed integer bit I should modify the ```for``` loop as ```for(bit_size=30; bit_size>=0; bit_size--)``` ? – Nitin Jadhav May 08 '19 at 14:51
  • No. – *`data[i] >> bit_size;`* – is implementation-defined and not guaranteed to be the same on all platforms. – Swordfish May 08 '19 at 14:52
  • Yet I am not clear what's wrong in that. In question as mentioned, he is referring 32bit integer, so I am using a variable ```bit_size``` whose value is set using ```for``` loop and used to right shift the data. Request you please elaborate or help with a web-link explaining this. Thanks in advance. – Nitin Jadhav May 08 '19 at 15:05
  • *and used to right shift the data.* – https://stackoverflow.com/a/4009954/3975177 – Swordfish May 08 '19 at 15:07