Author Topic: Select a random file to be read with TTS  (Read 3277 times)

kenjiro

  • Guest
Select a random file to be read with TTS
« on: October 10, 2017, 07:37:39 AM »
I use my VA profile while playing Elite Dangerous and I have created sort of a "galaxypedia", using TXT files. It works just fine if I issue something like "tell me about neutron stars".

But since its galaxypedia has 242 topics (at least until now), we would have to remember all the topics in order to ask for a particular one. I want to creat a command like "tell me about a random topic" and VA would randomly pick and read a file from the "galaxypedia" folder.

Is it already possible to be done in VA? Or would this be a future feature? I am no programmer, but since VA alreay does "play random sound", I believe it wouldn't be too hard to implement a "read random file" :)

Thanks for the attention
« Last Edit: October 10, 2017, 08:40:33 AM by kenjiro »

kenjiro

  • Guest
Re: Select a random file to be read with TTS
« Reply #1 on: October 10, 2017, 11:41:04 AM »
... and yes, I know VA randomizes the texts if I use something like "hello brother;hi friend;hey mom" (where it will pick ONE of the three phrases each time the command runs). But to use VA that way all the content of my galaxypedia would have to be in one "big" file, where the topics would be separated by ";".

That's not what I would like :(

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Select a random file to be read with TTS
« Reply #2 on: October 10, 2017, 01:48:53 PM »
You could use the "{TXTRANDOM:}" token and pass that to the "Set a Text Value" action's "Value from file/URI" feature to pick a text file.

E.G.
Code: [Select]
Set Text [~Topic] to [{VA_DIR}\Textfiles\{TXTRANDOM:topicA;topicB;topicC;TopicETC}.txt]
Say, '{TXT:~Topic}'


Alternatively, you can use a C# snippet to put all the files in a given directory into variables, and randomly choose one of those:

Code: [Select]
Set Text [~filePath] to '{VA_DIR}\Textfiles\'
Inline C# Function: Return files in directory, wait until execution finishes
Set small int (condition) [~rand] value to the converted value of {RANDOM:1:{SMALL:~randmax}}
Set Text [~Topic] to [{TXT:~File{SMALL:~rand}}]
Say, '{TXT:~Topic}'

Where the "Return files in directory" inline C# function contains:
Code: [Select]
using System;
using System.IO;

public class VAInline
{
public void main()
{
            string path = VA.GetText("~filePath");
            //VA.WriteToLog("Path: " + path, "red");
            if(Directory.Exists(path))
            {
                string [] fileEntries = Directory.GetFiles(path);
               
                short count = 0;
                foreach(string fileName in fileEntries)
                {
                    count++;
                    VA.SetText("~file" + count, fileName);
                    //VA.WriteToLog("File" + count + ": " + fileName, "red");
                }
                VA.SetSmallInt("~randmax", count);
                //VA.WriteToLog("Count: " + count, "red");
            }
            else
            {
                VA.WriteToLog("Path is not a valid directory", "red");
            }
}
}

Gangrel

  • Caffeine Fulled Mod
  • Global Moderator
  • Full Member
  • *****
  • Posts: 216
  • BORK FNORK BORD
Re: Select a random file to be read with TTS
« Reply #3 on: October 10, 2017, 01:55:25 PM »
I have used AutoHotKey to do something similar.

In my case it was to play a random line from a file. However, I can see it easily extendable to copy the text into clipboard, and then getting VA to say that.

kenjiro

  • Guest
Re: Select a random file to be read with TTS
« Reply #4 on: October 10, 2017, 02:21:49 PM »
You could use the "{TXTRANDOM:}" token and pass that to the "Set a Text Value" action's "Value from file/URI" feature to pick a text file.

E.G.
Code: [Select]
Set Text [~Topic] to [{VA_DIR}\Textfiles\{TXTRANDOM:topicA;topicB;topicC;TopicETC}.txt]
Say, '{TXT:~Topic}'


Well, today I got really busy around this idea (the random topics). So I went back to VA documentation and I did "stumbled" on {TXTRANDOM:value} ;)

Your reply ALMOST beat me there. But yes, thanks for replying, this might help someone else with the same doubt ;)

My random facts from the galaxypedia is working like a charm :D

Damn VA rocks \o/