What are chunks, samples and frames when using pyaudio
<p>After going through the documentation of pyaudio and reading some other articles on the web, I am confused if my understanding is correct.</p>

<p>This is the code for audio recording found on pyaudio's site:</p>

<pre><code>import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()
</code></pre>

<p>and if I add these lines then I am able to play whatever I recorded:</p>

<pre><code>play=pyaudio.PyAudio()
stream_play=play.open(format=FORMAT,
                      channels=CHANNELS,
                      rate=RATE,
                      output=True)
for data in frames: 
    stream_play.write(data)
stream_play.stop_stream()
stream_play.close()
play.terminate()
</code></pre>

<ol>
<li>"RATE" is the number of samples collected per second.</li>
<li>"CHUNK" is the number of frames in the buffer.</li>
<li>Each frame will have 2 samples as "CHANNELS=2".</li>
<li>Size of each sample is 2 bytes, calculated using the function: <code>pyaudio.get_sample_size(pyaudio.paInt16)</code>.</li>
<li>Therefore size of each frame is 4 bytes.</li>
<li>In the "frames" list, size of each element must be 1024*4 bytes, for example, size of <code>frames[0]</code> must be 4096 bytes. However, 
<code>sys.getsizeof(frames[0])</code> returns <code>4133</code>, but <code>len(frames[0])</code> returns <code>4096</code>.</li>
<li><code>for</code> loop executes <code>int(RATE / CHUNK * RECORD_SECONDS)</code> times, I cant understand why. <a href="https://dsp.stackexchange.com/questions/13728/what-are-chunks-when-recording-a-voice-signal/24249#24249?newreg=f46f1af2f2e84ba1bceb57fb257d8587">Here</a> is the same question answered by "Ruben Sanchez" but I cant be sure if its correct as he says <code>CHUNK=bytes</code>. And according to his explanation, it must be <code>int(RATE / (CHUNK*2) * RECORD_SECONDS)</code> as <code>(CHUNK*2)</code> is the number of samples read in buffer with each iteration.</li>
<li>Finally when I write <code>print frames[0]</code>, it prints gibberish as it tries to treat the string to be ASCII encoded which it is not, it is just a stream of bytes. So how do I print this stream of bytes in hexadecimal using <code>struct</code> module? And if later, I change each of the hexadecimal value with values of my choice, will it still produce a playable sound?</li>
</ol>

<p>Whatever I wrote above was my understanding of the things and many of them maybe wrong.</p>