from panda3d.core import *
import direct.directbase.DirectStart
from direct.stdpy.file import open # Python's own "open" won't work with ramdisks
vfs = VirtualFileSystem.getGlobalPtr()
vfs.mount(VirtualFileMountRamdisk(), "/ramdisk", 0)
# a file
realfile = open("test.ogg", "rb").read()
ramfile = open("/ramdisk/a.ogg", "wb")
ramfile.write(realfile)
ramfile.close()
sound = loader.loadSfx("/ramdisk/a.ogg")
sound = None
vfs.deleteFile("/ramdisk/a.ogg")
# another file, but when I play I hear the same sound
realfile2 = open("test2.ogg", "rb").read()
ramfile2 = open("/ramdisk/a.ogg", "wb")
ramfile2.write(realfile2)
ramfile2.close()
sound2 = loader.loadSfx("/ramdisk/a.ogg")
sound2.play()
run()
?
EDIT:
Maybe I need to do this before loading another sound with the same name:
I am still not able to delete an audio file from ramdisk, then store and load another one from ramdisk which has the same name:
from panda3d.core import *
# change default audio engine
loadPrcFileData('', 'audio-library-name p3openal_audio')
import direct.directbase.DirectStart
# Python's own "open" won't work with ramdisks
from direct.stdpy.file import open
# read and write files with RAM like with disk
vfs = VirtualFileSystem.getGlobalPtr()
vfs.mount(VirtualFileMountRamdisk(), '/ramdisk', 0)
# get a file to RAM
data = open('musicbox.ogg', 'rb').read() # actually read from another source
file = open('/ramdisk/music.ogg', 'wb')
file.write(data)
file.close()
# now load from RAM
music = loader.loadMusic('/ramdisk/music.ogg')
music.play()
def loadAnother():
global music
# remove existing from RAM
music.stop()
vfs.deleteFile('/ramdisk/music.ogg')
base.musicManager.uncacheSound('/ramdisk/music.ogg')
# get a file to RAM
data = open('openclose.ogg', 'rb').read() # actually read from another source
file = open('/ramdisk/music.ogg', 'wb')
file.write(data)
file.close()
# now load from RAM, FAILS
music = loader.loadMusic('/ramdisk/music.ogg')
music.play()
base.accept('enter', loadAnother)
run()
Seems like that should work. I’m not 100% sure that the MusicManager.uncacheSound() function works as advertised, though; I’ve never used it before.
Why don’t you try to isolate whether the problem you’re experiencing is due to the use of the ramdisk or due to the use of the music manager caching behavior?
from panda3d.core import *
# change default audio engine
loadPrcFileData('', 'audio-library-name p3openal_audio')
import direct.directbase.DirectStart
import os
if os.path.isdir('ramdisk') == False: os.mkdir('ramdisk')
# get a file to -RAM- folder
data = open('musicbox.ogg', 'rb').read() # actually read from another source
file = open('ramdisk/music.ogg', 'wb')
file.write(data)
file.close()
# now load from -RAM- folder
music = loader.loadMusic('ramdisk/music.ogg')
music.play()
def loadAnother():
global music
# remove existing from -RAM- folder
music.stop()
base.musicManager.uncacheSound('ramdisk/music.ogg')
# get a file to -RAM- folder
data = open('openclose.ogg', 'rb').read() # actually read from another source
file = open('ramdisk/music.ogg', 'wb')
file.write(data)
file.close()
# now load from -RAM- folder, FAILS
music = loader.loadMusic('ramdisk/music.ogg')
music.play()
base.accept('enter', loadAnother)
run()