Page 1 of 1

MemoryError when read wave frames

Posted: Tue Jun 05, 2018 11:04 pm
by Falong
Hi,
when using openMV DAC to play wav file, MemoryError exception is raised in chunk.py when readframes function in wave.py module is called.
I got the chunk.py and wave.py and test wav file from below article:

https://docs.micropython.org/en/latest/ ... _skin.html

the tested wave file is near 9KB, 8-bit, 16KHZ.

def play(filename):
f = wave.open(filename, 'r')
total_frames = f.getnframes()
framerate = f.getframerate()

for position in range(0, total_frames, framerate):
f.setpos(position)
dac.write_timed(f.readframes(framerate), framerate)
delay(1000)

Is this MemoryError due to limited heap size? how to check how much heap available? the tested wav file is less than 9kB.
is there anyone succeed in play wav file with openMV?
Thanks,

Re: MemoryError when read wave frames

Posted: Tue Jun 05, 2018 11:53 pm
by kwagyeman
Hi, I see no reason why this doesn't work. However, we've never tested this.

Can you give use the exact line of code that error happened on?

Re: MemoryError when read wave frames

Posted: Wed Jun 06, 2018 7:22 am
by Falong
Please see the attached screenshot.
Thanks,

Re: MemoryError when read wave frames

Posted: Wed Jun 06, 2018 11:49 am
by iabdalkader
Falong wrote:
Tue Jun 05, 2018 11:04 pm
Is this MemoryError due to limited heap size? how to check how much heap available? the tested wav file is less than 9kB.
import gc

print("Free: %dKBs"%(gc.mem_free()//1024))

Note memory could be fragmented.

Please share the whole script(s)+wave file(s) in a .zip and I'll test them.

Re: MemoryError when read wave frames

Posted: Thu Jun 07, 2018 8:21 pm
by Falong
Can you please leave me your mail address, I'll mail you the script.
Also find when wav file is very small(tested with 1KB), it is ok.
Thanks,

Re: MemoryError when read wave frames

Posted: Thu Jun 07, 2018 8:46 pm
by kwagyeman
OpenMV at OpenMV . io

Re: MemoryError when read wave frames

Posted: Sat Jun 09, 2018 4:14 pm
by iabdalkader
This is just a fragmented memory issue, the scripts run fine on H7. I think most of memory is consumed by just loading/parsing/compiling the modules wave and chunk.

This seems to fix it:

Code: Select all

            frames = f.readframes(frame_rate)
            self.dac.write_timed(frames, frame_rate)
            del frames
            delay(1000)
            gc.collect()

Re: MemoryError when read wave frames

Posted: Sat Jun 09, 2018 5:13 pm
by kwagyeman
Are you on an M4?

Re: MemoryError when read wave frames

Posted: Mon Jun 11, 2018 10:16 pm
by Falong
Thanks!
It's on M7.

Re: MemoryError when read wave frames

Posted: Tue Jun 12, 2018 7:01 pm
by Falong
iabdalkader wrote:
Sat Jun 09, 2018 4:14 pm
This is just a fragmented memory issue, the scripts run fine on H7. I think most of memory is consumed by just loading/parsing/compiling the modules wave and chunk.

This seems to fix it:

Code: Select all

            frames = f.readframes(frame_rate)
            self.dac.write_timed(frames, frame_rate)
            del frames
            delay(1000)
            gc.collect()
Just tested, it seems not fix my issue on M7. do you have an estimate of memory cost of openmv itself excluding framebuffer?

Thanks

Re: MemoryError when read wave frames

Posted: Tue Jun 12, 2018 8:19 pm
by iabdalkader
Falong wrote:
Tue Jun 12, 2018 7:01 pm
Just tested, it seems not fix my issue on M7. do you have an estimate of memory cost of openmv itself excluding framebuffer?
Thanks
Hi,

Add a gc.collect at the end of detect_face_image:

Code: Select all

        self.check_head_sideway(rotate_angle)
        gc.collect()
And gc.collect() before reading frames:

Code: Select all

            gc.collect()
            frames = f.readframes(frame_rate)
            self.dac.write_timed(frames, frame_rate)
            del frames
            gc.collect()
            print("Free: %dKBs"%(gc.mem_free()/1024))
Unfortunately MicroPython's GC doesn't do very good defragmentation, reading bigger chunks if possible, will help.