[SOLVED] Dynamically adding files to the cache in Android

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
trajano
Regular
Posts: 60
Joined: Sun Jun 16, 2019 7:59 pm
Github: trajano
Contact:

[SOLVED] Dynamically adding files to the cache in Android

#1 Post by trajano » Tue Jul 16, 2019 2:34 am

How do you dynamically add files to the cache/ folder in Android? On the PC I can do the renpy.loader.get_path to get the real path but this fails on Android

Code: Select all

        def queue_blips(who, what, cps):
            """
            Queue the blips.  This creates a blip every other character and resets the blip when a comma or space is detected.
            If the CPS is higher frequency than the blip length, it switches to Wendy Oldbag mode where it just beeps as fast
            as it can.
            """

            computed_blip_file = "cache/%d.wav" % ( hash( (who, what, cps) ) )
            computed_blip_path = os.path.normpath(renpy.loader.get_path(computed_blip_file)).replace("\\", "/")
            if os.path.isfile(computed_blip_path):
                renpy.sound.play(computed_blip_file)
                return

            tokens = textsupport.tokenize(unicode(what))
            odd = False

            blipout = wave.open(computed_blip_path, "wb")
            blipout.setframerate(blip_framerate)
            blipout.setsampwidth(blip_sample_width)
            blipout.setnchannels(blip_channels)

            cps_stack = []
            # initial character gap

            def silence(seconds):
                silence_byte_length = seconds *  blip_framerate * 2
                return audioop.tostereo(b'\0' * int(silence_byte_length), 1, 1, 1)

            def blip(seconds):
                silence_byte_length = ((seconds - blip_length) *  blip_framerate ) * 2
                return blip_frames + audioop.tostereo(b'\0' * int(silence_byte_length), 1, 1, 1)

            blipout.writeframes(silence(1.0/cps))

            for token_type, token_text in tokens:
                if token_type == TEXT:
                    if blip_length > 1.0/cps:
                        # Wendy Oldbag Speed at this point assume 0.05 seconds and just keep on playing until it reaches the end of the segment.
                        beeps_needed = int(len(token_text) / cps / blip_length) * 2
                        for i in xrange(beeps_needed):
                            # queue.append("<from 0 to %0.3f>%s" % (blip_length, blip_sound))
                            blipout.writeframes(blip_frames)
                    elif cps == 0:
                        pass
                    else:
                        speed = 1.0/cps
                        for letter in token_text:
                            odd = not odd
                            if letter in ', ':
                                # queue.append("<silence %0.3f>" % speed)
                                blipout.writeframes(silence(speed))
                                odd = False
                            else:
                                if odd:
                                    # queue.append("<from 0 to %0.3f>%s" % (speed, blip_sound))
                                    blipout.writeframes(blip(speed))
                                else:
                                    # queue.append("<silence %0.3f>" % speed)
                                    blipout.writeframes(silence(speed))

                if token_type == TAG:
                    match_cps_multiplier = re.match( r'cps=\*([0-9\.]+)', token_text)
                    match_cps = re.match( r'cps=([0-9\.]+)', token_text)
                    match_close_cps = re.match( r'/cps', token_text)
                    if match_cps_multiplier:
                        cps_stack.append(cps)
                        cps *= float(match_cps_multiplier.group(1))
                    elif match_cps:
                        cps_stack.append(cps)
                        cps = float(match_cps.group(1))
                    elif match_close_cps:
                        cps = cps_stack.pop()
                    odd = False
            blipout.close()
            renpy.sound.play(computed_blip_file)
            # renpy.sound.queue(queue, clear_queue=True, tight=True)

Last edited by trajano on Tue Jul 16, 2019 2:55 pm, edited 1 time in total.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Dynamically adding files to the cache in Android

#2 Post by Remix » Tue Jul 16, 2019 1:43 pm

You *might* get away with using the save directory:

Code: Select all

define config.save_directory = "sub_folder_name"

# Note: That is often set in options.rpy based on the project name (near line 145)

#
# then, in the code that wants to use that directory, read the value with :
config.savedir # <-- full absolute path depending on platform (do Not use this to set the value)
Frameworks & Scriptlets:

trajano
Regular
Posts: 60
Joined: Sun Jun 16, 2019 7:59 pm
Github: trajano
Contact:

Re: Dynamically adding files to the cache in Android

#3 Post by trajano » Tue Jul 16, 2019 2:55 pm

I was able to write to the save directory, but Ren'py won't read the files from it in Android for media playback. I found a workaround using io.BytesIO the example I have is in my blip library viewtopic.php?f=51&t=55866

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], minyan