Old 03-13-2016, 07:10 PM   #1
pcartwright
Human being with feelings
 
Join Date: Jan 2009
Posts: 1,030
Default Reading text files in JSFX

Random question:

I'm trying to read a .txt file into JSFX, but calling file_string(handle,str) only seems to return the last row on the .txt file.

I've read through the JSFX documentation, but I'm a bit lost regarding the IO section.

Ideally, I would like to pull each row of a text field into part of an array within the JSFX. Is this possible?

EDIT:
I guess you can disregard the question. The code below accomplishes my goal:

Code:
  handle = file_open(slider1); //open file
  fileOpened = handle; // verify file opend successfully
  istext = file_text(handle);
  istext == 1 ? (
    //file_rewind(handle); //start at the beginning
    fileRewind = file_rewind(handle); //display result
    i = 0;
    tmpHandle = 1;
    while (tmpHandle == 1) (
      x[i] = file_string(handle, string[i]); 
      i = i + 1;
      tmpHandle = file_avail(handle); // display result, repeat loop if 1
    );
    i = 0;
  );
I would add that I got strange results when one of my strings started with the word "test." Is test a keyword in the JSFX language?

Last edited by pcartwright; 03-13-2016 at 07:45 PM. Reason: issue solved
pcartwright is offline   Reply With Quote
Old 03-13-2016, 09:36 PM   #2
pcartwright
Human being with feelings
 
Join Date: Jan 2009
Posts: 1,030
Default

OK, scratch that. I'm getting some really weird results (missing rows, rows out of order, etc.). Has anyone been able to use .txt files with JSFX successfully?
pcartwright is offline   Reply With Quote
Old 03-13-2016, 11:09 PM   #3
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
Default

Quote:
Originally Posted by pcartwright View Post
OK, scratch that. I'm getting some really weird results (missing rows, rows out of order, etc.). Has anyone been able to use .txt files with JSFX successfully?
There are examples within the JSFXes that come with Reaper. e.g. MidiMapperV2 (or similar).

-Michael
mschnell is online now   Reply With Quote
Old 03-14-2016, 05:17 AM   #4
pcartwright
Human being with feelings
 
Join Date: Jan 2009
Posts: 1,030
Default

Quote:
Originally Posted by mschnell View Post
There are examples within the JSFXes that come with Reaper. e.g. MidiMapperV2 (or similar).

-Michael
I'm not seeing that one. Does it have a different name in 5.0?
pcartwright is offline   Reply With Quote
Old 03-14-2016, 02:03 PM   #5
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
Default

Quote:
Originally Posted by pcartwright View Post
I'm not seeing that one. Does it have a different name in 5.0?
"Midi Map to key v2 [Xix]"

Same seems to use a mapping file. (I did not try to find out how that works)

-Michael
mschnell is online now   Reply With Quote
Old 03-14-2016, 03:25 PM   #6
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,746
Default

Reading text files in JSFX predates JSFX having a string type, so really it's designed to be used to read numeric values from the text... rather than freeform strings.
Justin is offline   Reply With Quote
Old 03-14-2016, 06:18 PM   #7
pcartwright
Human being with feelings
 
Join Date: Jan 2009
Posts: 1,030
Default

Quote:
Originally Posted by Justin View Post
Reading text files in JSFX predates JSFX having a string type, so really it's designed to be used to read numeric values from the text... rather than freeform strings.
Thanks for the info, Justin.

That being the case, what is the primary function of file_string(handle,str)? Assuming file_string is not a good solution, is it possible to read in all text to one string for manipulation later?
pcartwright is offline   Reply With Quote
Old 03-14-2016, 08:41 PM   #8
pcartwright
Human being with feelings
 
Join Date: Jan 2009
Posts: 1,030
Default

Quote:
Originally Posted by mschnell View Post
"Midi Map to key v2 [Xix]"

Same seems to use a mapping file. (I did not try to find out how that works)

-Michael
Thanks. I'll have a look.
pcartwright is offline   Reply With Quote
Old 03-15-2016, 03:15 AM   #9
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
Default

Quote:
Originally Posted by Justin View Post
Reading text files in JSFX predates JSFX having a string type, so really it's designed to be used to read numeric values from the text... rather than freeform strings.
Now, as we have the string type would it not be very viable to add this functionality ? Of course writing text files would be welcome, as well.

-Michael
mschnell is online now   Reply With Quote
Old 03-15-2016, 07:48 AM   #10
pcartwright
Human being with feelings
 
Join Date: Jan 2009
Posts: 1,030
Default

Ok, I think I understand how this works now and have a way around the issue. Instead of creating files structured like this:
OldParameter1 OldParameter2 OldParameter3
NewParameter1 NewParameter2 NewParameter3
...

I should write files structured like this:
OldParameter1
OldParameter2
OldParameter3

NewParameter1
NewParameter2
NewParameter3

...

This way, I can use file_var instead of file_string.

Does this sound correct?
pcartwright is offline   Reply With Quote
Old 03-15-2016, 08:20 PM   #11
pcartwright
Human being with feelings
 
Join Date: Jan 2009
Posts: 1,030
Default

Well... Turns out there was an issue with my index memory code in the init section that caused my issues... file_string works as expected. Are there any best practices for JSFX memory indexing where multiple indexes are needed?
pcartwright is offline   Reply With Quote
Old 03-16-2016, 12:29 AM   #12
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
Default

No automatic for this. A very usual issue

Doing a multi-instance JSFX with communication via common memory this hurts even more....

-Michael
mschnell is online now   Reply With Quote
Old 03-16-2016, 12:05 PM   #13
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,891
Default

Quote:
Originally Posted by pcartwright View Post
Are there any best practices for JSFX memory indexing where multiple indexes are needed?
Yeah, be careful.
IXix is offline   Reply With Quote
Old 03-17-2016, 03:09 PM   #14
ijijn
Human being with feelings
 
ijijn's Avatar
 
Join Date: Apr 2012
Location: Christchurch, New Zealand
Posts: 482
Default

Haha, that's absolutely right.

And how about: write custom wrapper functions to handle things cleanly and make sure they're working properly, with special emphasis on testing the first, the last and somewhere-in-the-middle cases. These days when I'm refactoring and generally making things tidier, I do a search for []s in the body of my code and make sure they are encapsulated away in functions up the top. It's my []->() meditation.

Also, if in doubt, draw a map.

Finally, remember that local variables inside functions will have separate value streams within @init, @block, @gfx, etc. They are essentially different variables in each context. This has caught me out on a few occasions when doing routine housekeeping.
ijijn is offline   Reply With Quote
Old 09-19-2016, 08:15 PM   #15
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,746
Default

Quote:
Originally Posted by Justin View Post
Reading text files in JSFX predates JSFX having a string type, so really it's designed to be used to read numeric values from the text... rather than freeform strings.
Ooops I was thinking of file_text() there, disregard my previous post!
Justin is offline   Reply With Quote
Old 06-28-2017, 12:44 PM   #16
Erriez
Human being with feelings
 
Join Date: Jun 2010
Location: The Netherlands
Posts: 179
Default

I had exactly the same problem to read strings from a file. The code below always reads the current line of the file position and ignores the variable i array index:
Code:
// Does not work:
file_string(file_handle, str[i]);
After a few hours investigation I found a workaround:
Code:
// Use a separate variable str to read strings from a file:
str = 0;
i = 0;
while (
	// Read string from file
	file_string(file_handle, str);
	
	// Copy string from file to a strings array
	strings[i] = str;
	
	// Increment string pointer
	str += 1;
	
	// Increment index in strings array
	i += 1;
	
	// Check EOF (end of file)
	file_avail(file_handle) > 0;
);        
file_close(file_handle);

A full JS script example:


Code:
//
// This demo reads an ASCII text file, selectable with a slider
//
// Create two text files with content in:
//     AppData\Roaming\REAPER\Data\read_files\File1.txt
//         // This is file 1
//         [Kick]
//         35
//         36
//     AppData\Roaming\REAPER\Data\read_files\File2.txt
//         // This is file 2
//         [Boom]
//         Another string
//         Here I am: 234
//         Last and fith line
//
// Run this example as an effect on a track

desc: Read strings from file example

slider1:/read_files:none:File:

//////////////////////////////////////////////////////////////////////
@init

// Memory organisation of the big data array:
//  0 1 2 3   4 5 6 7 8  9 10 11 12 . . .
//  0 1 2 3   0 1 2 3 4  0  1  2  3 
//  \-----/   \-------/  \--------/
// strings1   strings2    strings3
//

// Fixed strings1 array in memory
strings1 = 0; // Start at 0
strings1[0] = "First line";
strings1[1] = "Second";
strings1[2] = "And third";
strings1[3] = "Last one";

num_strings1 = 4; // 4 elements in array

// Read strings from file and overwrite existing strings2 array:
strings2 = 4; // Start at 4
strings2[0] = "Overwrite me";
strings2[1] = "def";
strings2[2] = "ghi";
strings2[3] = "jkl";
strings2[4] = "Last overwrite";

num_strings2 = 5; // 5 elements in array

// Fixed strings array in memory
strings3 = 9;  // Start at 9
strings3[0] = "---";
strings3[1] = "4th";
strings3[2] = "Last in column";
strings3[3] = "---";

num_strings3 = 4; // 6 elements in array

// Total number of strings
num_strings = num_strings1 + num_strings2 + num_strings3;

// Read file once at startup
file_selected = -1;

@slider
// Read file only on slider change
(file_selected != slider1) ? (
    file_selected = slider1;
       
    // Open the file
    file_handle = file_open(slider1);
    
    // Check file handle and text file
    (file_handle > 0) && file_text(file_handle) ?
    (
        // Use a separate variable to read strings from a file
        str = 0; // Start at 0
        
        // Start with 0 strings
        num_strings2 = 0;
        while (
            // Read string from file
            file_string(file_handle, str);
            
            // Copy string from file to the middle of the data array
            strings2[num_strings2] = str;
            
            // Increment string pointer
            str += 1;
            
            // Increment number of strings
            num_strings2 += 1;
            
            // Check EOF (end of file)
            file_avail(file_handle) > 0;
        );        
        file_close(file_handle);
    );
);

//-----------------------------------------------------------------------
@gfx 800, 500

// Graphics initialisation
gfx_a = 1;
gfx_mode = 0;
gfx_r = 1; gfx_g = 1; gfx_b = 1;
gfx_setfont(1, "Courier New", 20);

function DrawNumber(x, y, number)
(
    gfx_x = x; gfx_y = y;
    gfx_drawnumber(number, 0);
);

function DrawText(x, y, text)
(
    gfx_x = x; gfx_y = y;
    gfx_drawstr(text);
);

//-----------------------------------------------
DrawText(10, 10, "Strings1:");
DrawText(250, 10, "Strings2:");
DrawText(520, 10, "Strings3:");

i = 0;
loop(num_strings1,
    DrawText(30, 30+(20*i), strings1[i]);
    i += 1;
);

i = 0;
loop(num_strings2,
    DrawText(270, 30+(20*i), strings2[i]);
    i += 1;
);

i = 0;
loop(num_strings3,
    DrawText(550, 30+(20*i), strings3[i]);
    i += 1;
);

//-----------------------------------------------
gfx_line(0, 150, gfx_w, 150);

DrawText(10, 180, "Array memory organisation:");

my_string = 0;
i = 0;
loop(num_strings,
    DrawNumber(10, 200+(20*i), i);
    DrawText(50, 200+(20*i), my_string[i]);
    i += 1;
);

Last edited by Erriez; 06-28-2017 at 01:00 PM.
Erriez is offline   Reply With Quote
Old 06-29-2017, 10:47 AM   #17
James HE
Human being with feelings
 
James HE's Avatar
 
Join Date: Mar 2007
Location: I'm in a barn
Posts: 4,467
Default

There also is a bit of example code in ReaPack. The functions put the lines of text from a text file into numbered strings.

This is good unless you have hundreds of strings (i think the numbereds string limit is 64??), you avoid having to index things into memory.

https://github.com/ReaTeam/ReaScript...%20string.jsfx


thread: http://forum.cockos.com/showthread.p...87#post1733587
James HE is offline   Reply With Quote
Old 07-01-2017, 01:52 PM   #18
Erriez
Human being with feelings
 
Join Date: Jun 2010
Location: The Netherlands
Posts: 179
Default

Quote:
Originally Posted by James HE View Post
There also is a bit of example code in ReaPack. The functions put the lines of text from a text file into numbered strings.

This is good unless you have hundreds of strings (i think the numbereds string limit is 64??), you avoid having to index things into memory.

https://github.com/ReaTeam/ReaScript...%20string.jsfx


thread: http://forum.cockos.com/showthread.p...87#post1733587
Thanks, that's very useful. Now I can parse strings and numbers from ASCII configuration files.
https://github.com/Erriez/erriez-reaper-jsfx


Last edited by Erriez; 07-01-2017 at 01:59 PM. Reason: Fix typo in link
Erriez is offline   Reply With Quote
Old 07-01-2017, 04:37 PM   #19
James HE
Human being with feelings
 
James HE's Avatar
 
Join Date: Mar 2007
Location: I'm in a barn
Posts: 4,467
Default

Quote:
Originally Posted by Erriez View Post
Thanks, that's very useful. Now I can parse strings and numbers from ASCII configuration files.
https://github.com/Erriez/erriez-reaper-jsfx

looking great!
James HE 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 11:08 AM.


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