2

I am using PyAudio to detect an audio source's activity. I am creating WAV files from each audio event on a stream that is often dead. Using memory_profiler I noticed that the pack method in the record_to_file() method often uses 4x the amount of memory than I can regain by deleteing the data object. This code is taken from Detect & Record Audio in Python

from sys import byteorder
import sys
from array import array
import struct

import gc

import pyaudio
import wave
import subprocess
import objgraph
from memory_profiler import profile
from guppy import hpy

THRESHOLD = 5000
CHUNK_SIZE = 1024
FORMAT = pyaudio.paInt16
RATE = 44100

def is_silent(snd_data):
    "Returns 'True' if below the 'silent' threshold"
    return max(snd_data) < THRESHOLD


def normalize(snd_data):
    "Average the volume out"
    MAXIMUM = 16384
    times = float(MAXIMUM)/max(abs(i) for i in snd_data)

    r = array('h')
    for i in snd_data:
        r.append(int(i*times))
    return r


def trim(snd_data):
    "Trim the blank spots at the start and end"
    def _trim(snd_data):
        snd_started = False
        r = array('h')

        for i in snd_data:
            if not snd_started and abs(i)>THRESHOLD:
                snd_started = True
                r.append(i)

            elif snd_started:
                r.append(i)
        return r

    # Trim to the left
    snd_data = _trim(snd_data)

    # Trim to the right
    snd_data.reverse()
    snd_data = _trim(snd_data)
    snd_data.reverse()
    return snd_data


def add_silence(snd_data, seconds):
    "Add silence to the start and end of 'snd_data' of length 'seconds' (float)"
    r = array('h', [0 for i in xrange(int(seconds*RATE))])
    r.extend(snd_data)
    r.extend([0 for i in xrange(int(seconds*RATE))])
    return r


def record():
    """g
    Record a word or words from the microphone and 
    return the data as an array of signed shorts.

    Normalizes the audio, trims silence from the 
    start and end, and pads with 0.5 seconds of 
    blank sound to make sure VLC et al can play 
    it without getting chopped off.
    """
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT, channels=1, rate=RATE,
        input=True, output=True,
        frames_per_buffer=CHUNK_SIZE)

    num_silent = 0
    snd_started = False

    r = array('h')

    while 1:
        # little endian, signed short
        snd_data = array('h', stream.read(CHUNK_SIZE))
        if byteorder == 'big':
            snd_data.byteswap()
        r.extend(snd_data)

        silent = is_silent(snd_data)

        if silent and snd_started:
            num_silent += 1
        elif not silent and not snd_started:
            snd_started = True

        if snd_started and num_silent > 400:
            break

    sample_width = p.get_sample_size(FORMAT)
    stream.stop_stream()
    stream.close()
    p.terminate()

    #r = normalize(r)
    r = trim(r)
    r = add_silence(r, 0.5)

    return sample_width, r

@profile
def record_to_file(path):
    "Records from the microphone and outputs the resulting data to 'path'"
    sample_width, data = record()

    data = struct.pack('<' + ('h'*len(data)), *data)
    print(sys.getsizeof(data))
    wf = wave.open(path, 'wb')
    wf.setnchannels(1)
    wf.setsampwidth(sample_width)
    wf.setframerate(RATE)
    wf.writeframes(data)
    wf.close()
    del data
    gc.collect()


if __name__ == '__main__':
    count = 1
    h = hpy()
    f = open('heap.txt','w')
    objgraph.show_growth(limit=3) 
    while(1):
        filename = 'demo' + str(count) + '.wav'
        print("please speak a word into the microphone")
        record_to_file(filename)
        print("done - result written to {0}".format(filename))
        cmd = 'cd "C:\\Users\\user\\Desktop\\Raudio" & ffmpeg\\bin\\ffmpeg -i {0} -acodec libmp3lame {1}.mp3'.format(filename, filename)
        "subprocess.call(cmd, shell=True)"
        count += 1
        objgraph.show_growth()
        print h.heap()

Here is one iteration of output from the memory profiler module:

Line #    Mem usage    Increment   Line Contents
================================================
   117     19.6 MiB      0.0 MiB   @profile
   118                             def record_to_file(path):
   119                                 "Records from the microphone and outputs the resulting data to 'path'"
   120     22.4 MiB      2.8 MiB       sample_width, data = record()
   125     31.3 MiB      8.9 MiB       data = struct.pack('<' + ('h'*len(data)), *data)
   126     31.3 MiB      0.0 MiB       print(sys.getsizeof(data))
   127     31.3 MiB      0.0 MiB       wf = wave.open(path, 'wb')
   128     31.3 MiB      0.0 MiB       wf.setnchannels(1)
   129     31.3 MiB      0.0 MiB       wf.setsampwidth(sample_width)
   130     31.4 MiB      0.0 MiB       wf.setframerate(RATE)
   131     31.4 MiB      0.0 MiB       wf.writeframes(data)
   132     31.4 MiB      0.0 MiB       wf.close()
   133     30.4 MiB     -0.9 MiB       del data
   134     27.9 MiB     -2.5 MiB       gc.collect()

Debugging the process with VS, I am seeing a very large amount of 'h' characters in my system memory, which likely why the leak is occurring. Any help would be greatly appreciated

1 Answer 1

4

The struct class keeps a cache of items for faster access. The only way to clear the struct cache is to call the struct._clearcache() method. An example of this being used can be found here.

Warning! This is a _ method and could change at any time. See here for and explanation of these types of methods.

There is a discussion about this 'memory leak' on the python forums here and here.

3
  • Thank you! Exactly what I needed. I assume it would have called that automatically if it was low on memory. What do you mean by change any time? Like between code releases? Nov 2, 2017 at 15:05
  • Yeah. The method might break after a new release with no notice from the library dev.
    – PeterH
    Nov 2, 2017 at 22:24
  • A method starting with _ is not really supposed to be used outside of it's library. In python there are no 'private' methods like in java or c++. For example in Java if a dev doesn't want anyone to be able to access a method in a class they can mark it as private and the language will take care of blocking access to the method, but in python there is no such thing. There is just a naming convention. If a method name starts with _ or __ it should only be used inside of it's library because the dev does not guarantee that the method will stay the same between releases, or just be removed.
    – PeterH
    Nov 2, 2017 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.