Thread: reading 16 bit ints via stdin

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    2

    reading 16 bit ints via stdin

    Hi All;

    Im looking to read a stream of 16 bit signed ints into a buffer, then pass that buffer off to a function. Another program provides raw data as 16 bit signed integers, and I pipe them through to stdin via command line.

    This is what I have so far, and while it does not error, I am not getting data back that i would expect from the function. Im thinking its not correctly passing the buffer to the function, or its type is not right. Fairly new to C.

    Code:
    #include "fsync_decode.h"
    #include <fcntl.h> 
    #include <stdio.h> 
    #include <string.h>
    #include <unistd.h> 
    #include <stdlib.h>
    #include <stdint.h>
    
    main() 
    { 
    fsync_decoder_t *decoder; 
    int result; 
    int samplerate = 22050;
    short buffer[1024];
    
    decoder = fsync_decoder_new(samplerate); 
    
    while(1) 
        { 
        read(0, buffer, 1024);
        // Pass samples to function, will return 0 if it needs more samples
        // Returns -1 if there is an error.
        int result = fsync_decoder_process_samples(decoder, buffer, sizeof(buffer)); 
        switch(result) 
            { 
                case 0: break; 
                case -1: exit(1); 
            } 
        } 
    }
    The function belongs to a library which has been used elsewhere and works.. link: GitHub - atmatthewat/fsync-encode-decode: Software modem for a specific data burst format

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I assume you defined FSYNC_SAMPLE_FORMAT_S16 when compiling the library.

    For the read call, the size should be sizeof(buffer) (which will be 1024 * 2).
    For the decoder call, it should be sizeof(buffer) / 2 since it probably wants the number of samples, not the number of bytes.
    And you are of course assuming that short is 16 bits, which is likely but depends on your system.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    2
    Quote Originally Posted by algorism View Post
    I assume you defined FSYNC_SAMPLE_FORMAT_S16 when compiling the library.

    For the read call, the size should be sizeof(buffer) (which will be 1024 * 2).
    For the decoder call, it should be sizeof(buffer) / 2 since it probably wants the number of samples, not the number of bytes.
    And you are of course assuming that short is 16 bits, which is likely but depends on your system.
    That was the set of fresh eyes I needed-thanks. I don't know why I omitted a 16 bit int is 2 bytes so buffer/2 = samples. I'll give it a shot.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading ints and doubles using scanf
    By Gallivant in forum C Programming
    Replies: 8
    Last Post: 04-24-2015, 09:14 AM
  2. reading 2 ints and a char
    By wearld97 in forum C Programming
    Replies: 20
    Last Post: 11-11-2011, 02:22 PM
  3. reading 3 ints from one line, then 3 from another
    By Tokay in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2005, 09:42 PM
  4. Reading an unknown # of ints into array
    By jds in forum C Programming
    Replies: 3
    Last Post: 10-10-2003, 04:16 PM

Tags for this Thread