|
|
|
03-13-2016, 07:10 PM
|
#1
|
Human being with feelings
Join Date: Jan 2009
Posts: 1,030
|
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
|
|
|
03-13-2016, 09:36 PM
|
#2
|
Human being with feelings
Join Date: Jan 2009
Posts: 1,030
|
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?
|
|
|
03-13-2016, 11:09 PM
|
#3
|
Human being with feelings
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
|
Quote:
Originally Posted by pcartwright
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
|
|
|
03-14-2016, 05:17 AM
|
#4
|
Human being with feelings
Join Date: Jan 2009
Posts: 1,030
|
Quote:
Originally Posted by mschnell
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?
|
|
|
03-14-2016, 02:03 PM
|
#5
|
Human being with feelings
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
|
Quote:
Originally Posted by pcartwright
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
|
|
|
03-14-2016, 03:25 PM
|
#6
|
Administrator
Join Date: Jan 2005
Location: NYC
Posts: 15,746
|
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.
|
|
|
03-14-2016, 06:18 PM
|
#7
|
Human being with feelings
Join Date: Jan 2009
Posts: 1,030
|
Quote:
Originally Posted by Justin
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?
|
|
|
03-14-2016, 08:41 PM
|
#8
|
Human being with feelings
Join Date: Jan 2009
Posts: 1,030
|
Quote:
Originally Posted by mschnell
"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.
|
|
|
03-15-2016, 03:15 AM
|
#9
|
Human being with feelings
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
|
Quote:
Originally Posted by Justin
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
|
|
|
03-15-2016, 07:48 AM
|
#10
|
Human being with feelings
Join Date: Jan 2009
Posts: 1,030
|
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?
|
|
|
03-15-2016, 08:20 PM
|
#11
|
Human being with feelings
Join Date: Jan 2009
Posts: 1,030
|
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?
|
|
|
03-16-2016, 12:29 AM
|
#12
|
Human being with feelings
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
|
No automatic for this. A very usual issue
Doing a multi-instance JSFX with communication via common memory this hurts even more....
-Michael
|
|
|
03-16-2016, 12:05 PM
|
#13
|
Human being with feelings
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,891
|
Quote:
Originally Posted by pcartwright
Are there any best practices for JSFX memory indexing where multiple indexes are needed?
|
Yeah, be careful.
|
|
|
03-17-2016, 03:09 PM
|
#14
|
Human being with feelings
Join Date: Apr 2012
Location: Christchurch, New Zealand
Posts: 482
|
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.
|
|
|
09-19-2016, 08:15 PM
|
#15
|
Administrator
Join Date: Jan 2005
Location: NYC
Posts: 15,746
|
Quote:
Originally Posted by Justin
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!
|
|
|
06-28-2017, 12:44 PM
|
#16
|
Human being with feelings
Join Date: Jun 2010
Location: The Netherlands
Posts: 179
|
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.
|
|
|
07-01-2017, 01:52 PM
|
#18
|
Human being with feelings
Join Date: Jun 2010
Location: The Netherlands
Posts: 179
|
Quote:
Originally Posted by James HE
|
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
|
|
|
07-01-2017, 04:37 PM
|
#19
|
Human being with feelings
Join Date: Mar 2007
Location: I'm in a barn
Posts: 4,467
|
Quote:
Originally Posted by Erriez
|
looking great!
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 11:08 AM.
|