Thread: Audio Bitrate Calculation

  1. #1
    Banned
    Join Date
    Jul 2022
    Posts
    112

    Audio Bitrate Calculation

    The output value should be 2.1 mb and not 2133337 mb ?


    Code:
    #include <stdio.h>
    
    
    static void file_size() {
        u_int32_t channels = 2;
        u_int32_t duration_in_second = 129;
        u_int32_t samples_per_second = 44100;
        u_int32_t kilobytes_per_second = 192;
    
    
        u_int64_t filesize_in_kilobytes = duration_in_second * kilobytes_per_second * samples_per_second * channels;
        
        printf("Audio File: %lld KB\n", filesize_in_kilobytes);
        printf("Audio File: %lld MB\n", filesize_in_kilobytes / 1024);
        return;
    }
    
    
    int main(int argc, const char *argv[]) {
        file_size();
        return 0;
    }
    
    
    #OUTPUT
    Audio File: 2184537600 KB
    Audio File: 2133337 MB

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,347
    > u_int32_t duration_in_second = 129;
    > u_int32_t kilobytes_per_second = 192;
    192 x 129 is never going to be 2.1MB

    Your calculation is plain wrong.
    channels * bytes per sample * samples per second * seconds.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Quote Originally Posted by Salem View Post
    > u_int32_t duration_in_second = 129;
    > u_int32_t kilobytes_per_second = 192;
    192 x 129 is never going to be 2.1MB

    Your calculation is plain wrong.
    channels * bytes per sample * samples per second * seconds.
    Code:
    static void file_size() {
        u_int32_t channels = 2;
        u_int32_t duration_in_second = 129;
        u_int32_t samples_per_second = 44100;
        u_int32_t bytes_per_sample = 192 * 1024;
    
    
        u_int64_t filesize_in_bytes = channels * bytes_per_sample * samples_per_second * duration_in_second;
    
    
        printf("Audio File: %lld Bytes\n", filesize_in_bytes);
        return;
    }
    
    #OUTPUT
    Audio File: 3583508480 Bytes
    Certainly the value is not bytes ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,347
    bytes per sample is like 2 usually.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,021
    This will overflow:
    Code:
    u_int64_t filesize_in_bytes = channels * bytes_per_sample * samples_per_second * duration_in_second;
    Example:
    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    uint64_t f( uint32_t a, uint32_t b, uint32_t c, uint32_t d )
    { return a * b * c * d; }
    
    uint64_t g( uint32_t a, uint32_t b, uint32_t c, uint32_t d )
    { return (uint64_t)a * b * c * d; }
    
    int main( void )
    {
      printf( "%" PRIu64 ", %" PRIu64 "\n",
        f( 2, 129, 44100, 192*1024 ),  
        g( 2, 129, 44100, 192*1024 ) );  
    }
    Results in:
    Code:
    $ cc -o test test.c
    $ ./test
    3583508480, 2236966502400
    Last edited by flp1969; 07-29-2022 at 08:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing raw audio file from ALSA audio buffer
    By hexstrauss in forum Windows Programming
    Replies: 1
    Last Post: 02-24-2021, 07:13 AM
  2. Replies: 3
    Last Post: 09-08-2017, 07:53 PM
  3. Audio
    By fighter92 in forum Game Programming
    Replies: 9
    Last Post: 04-04-2009, 03:01 PM
  4. Audio
    By gvector1 in forum C# Programming
    Replies: 7
    Last Post: 06-26-2003, 08:11 AM
  5. Audio
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 06-01-2002, 10:05 PM

Tags for this Thread