Old 10-30-2014, 01:00 PM   #1
Argitoth
Human being with feelings
 
Argitoth's Avatar
 
Join Date: Feb 2008
Location: Mesa, AZ
Posts: 2,057
Default memory leak with audio accessor

grab your favorite trousers and run this script on thousands of wav files so you can see the memory go up up and awaayyyy!

As you can see from my comments I tried some memory management stuff, but nothing worked.

Bug?

Code:
from reaper_python import *
from contextlib import contextmanager
# import gc

def msg(msg): RPR_ShowConsoleMsg(msg)

@contextmanager
def undoable(message):
		# gc.disable();
		RPR_Undo_BeginBlock()
		try: yield
		finally:
			RPR_Undo_EndBlock(message, -1)
			# gc.enable()
 
def script():
	title = "Trim Silence"
	inputs = 3
	names = "threshold(dB),threshold(sec),skip first x sec"
	defaults = "-90,.1,.05"
	Query = RPR_GetUserInputs(title,inputs,names,defaults,200)
	if Query[0] == 1:		
		items = RPR_CountSelectedMediaItems(0)		
		ItemList = []
		for i in range(items):
				ItemList.append(RPR_GetSelectedMediaItem(0, i)) 
		for i in range(items): 
				# get item info
				item       = ItemList[i]
				track      = RPR_GetMediaItemTrack(item)
				take       = RPR_GetActiveTake(item)
				source     = RPR_GetMediaItemTake_Source(take)
				samplerate = RPR_GetMediaSourceSampleRate(source)
				position   = RPR_GetMediaItemInfo_Value(item, "D_POSITION")
				length     = RPR_GetMediaItemInfo_Value(item, "D_LENGTH")				
				offset     = 0 #RPR_GetMediaItemTakeInfo_Value(take, "D_STARTOFFS")	
				channels   = 1 #RPR_GetMediaSourceNumChannels(source)				

				# user values	
				Input        = Query[4].split(',')
				speederupper = 1000
				minAmp       = 2**(float(Input[0])/6) 
				maxTime      = float(Input[1])*samplerate/speederupper
				skipTime     = float(Input[2])
				samples      = int((length-offset-skipTime)*samplerate)

				# msg("item = "+str(item)+"\n")
				# msg("take = "+str(take)+"\n")
				# msg("length = "+str(length)+"\n")
				# msg("offset = "+str(offset)+"\n")
				# msg("skiptime = "+str(skipTime)+"\n")
				# msg("length-offset-skipTime = "+str(length-offset-skipTime)+"\n")
				# msg("samplerate = "+str(samplerate)+"\n")
				# msg("channels = "+str(channels)+"\n")
				# msg("samples = "+str(samples)+"\n")
				# msg("minAmp = "+str(minAmp)+"\n\n")
				# msg("maxTimeSamps = "+str(maxTime*speederupper)+"\n")
				
				# take buffer
				BufferTake = list([0]*channels*samples)
				accessorTake   = RPR_CreateTakeAudioAccessor(take)
				(p,BufferTake) = RPR_GetAudioAccessorSamples(accessorTake, samplerate, channels, offset+skipTime, samples, BufferTake)
				RPR_DestroyAudioAccessor(accessorTake)

				# msg("accessorTake = "+str(accessorTake)+"\n")
				# msg("accessorState = "+str(p)+"\n") 

				# track buffer
				# BufferTrack     = list([0]*channels*samples)
				# accessorTrack   = RPR_CreateTrackAudioAccessor(track)
				# (p,BufferTrack) = RPR_GetAudioAccessorSamples(accessorTrack, samplerate, channels, position+skipTime, samples, BufferTrack)
				# RPR_DestroyAudioAccessor(accessorTrack)		

				# msg("accessorTrack = "+str(accessorTrack)+"\n")
				# msg("accessorState = "+str(p)+"\n")
			   
				s=0#sample
				t=0#time
				v=0#value
				
				while s < samples:
						v = abs(BufferTake[s])
						if v < minAmp: t+=1
						else: t=0
						if t > maxTime: break
						s+=speederupper			
				# while s < samples:
				# 		v = abs(BufferTrack[s])
				# 		if v < minAmp: t+=1
				# 		else: t=0
				# 		if t > maxTime: break
				# 		s+=speederupper
				# gc.collect()
				# del BufferTake[:]
 
				trimSilencePos = (s-maxTime)/samplerate+offset
				RPR_SetMediaItemLength(item, trimSilencePos, 1)
				msg("")
				msg("Removing silence: item "+str(i+1)+"/"+str(items)+" finished.")
				
				# msg("sample = "+str(s)+"\n")
				# msg("sampleVal = "+str(BufferTake[s])+"\n")
				# msg("sampleVal = "+str(BufferTrack[s])+"\n")
				# msg("trimSilencePos = "+str(trimSilencePos)+"\n")
 
with undoable("Trim silence"):		
		script()
__________________
Soundemote - Home of the chaosfly and pretty oscilloscope.
MyReaperPlugin - Easy-to-use cross-platform C++ REAPER extension template
Argitoth is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.