16 bit PCM Audio Sounds Like Bit Crushing
In case anyone has the same problem, I want to tell that the above code works really well. My problem about the distortion of audio caused by wrong buffer size. 

But while searching for a solution, I figured out that everyone says that using union may cause undefined behaviour in different CPU structures. It is said to use bitshift operations instead. So I decided to change the code for conversion as below:

Input Conversion

int16_t sample = 0;
    for (int k = 0; k &lt; BYTE_RESOLUTION_IN; k++) {
    sample |= input_buffer[i + k] &lt;&lt; (k * 8);
}


Output Conversion

int16_t sample_int16 = (int16_t) temp;
    for (int k = 0; k &lt; BYTE_RESOLUTION_OUT; k++) {
    output_buffer[i + k] = (sample_int16 &gt;&gt; (k * 8)) &amp; 0xFF;
}


And this conversion is made for Little-Endian system. If you have Big-Endian system, you have to change the k value properly.

Have a nice day