What are chunks, samples and frames when using pyaudio

后端 未结 1 1066
无人共我
无人共我 2020-12-13 01:17

After going through the documentation of pyaudio and reading some other articles on the web, I am confused if my understanding is correct.

This is the code for audio

1条回答
  •  时光说笑
    2020-12-13 01:39

    1. "RATE" is the "sampling rate", i.e. the number of frames per second
    2. "CHUNK" is the (arbitrarily chosen) number of frames the (potentially very long) signals are split into in this example
    3. Yes, each frame will have 2 samples as "CHANNELS=2", but the term "samples" is seldom used in this context (because it is confusing)
    4. Yes, size of each sample is 2 bytes (= 16 bits) in this example
    5. Yes, size of each frame is 4 bytes
    6. Yes, each element of "frames" should be 4096 bytes. sys.getsizeof() reports the storage space needed by the Python interpreter, which is typically a bit more than the actual size of the raw data.
    7. RATE * RECORD_SECONDS is the number of frames that should be recorded. Since the for loop is not repeated for each frame but only for each chunk, the number of loops has to be divided by the chunk size CHUNK. This has nothing to do with samples, so there is no factor of 2 involved.
    8. If you really want to see the hexadecimal values, you can try something like [hex(x) for x in frames[0]]. If you want to get the actual 2-byte numbers use the format string ' with the struct module.

    You might be interested in my tutorial about reading WAV files with the wave module, which covers some of your questions in more detail: http://nbviewer.jupyter.org/github/mgeier/python-audio/blob/master/audio-files/audio-files-with-wave.ipynb

    0 讨论(0)
提交回复
热议问题