Welcome to %s forums

BrainModular Users Forum

Login Register

CSV

I need help on a Patch
Post Reply
Ant1
New member
Posts: 4
Contact:

Unread post by Ant1 » 24 Oct 2017, 09:21

Hi everyone,

Does anyone in the community know whether it is possible to read .csv files in Usine. Or perhaps other text file formats, for the purpose of reading data collection and produce sonifications... thank you !

Ant1

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 24 Oct 2017, 14:27

have a look in script folder read/write textfile

sm_jamieson
Member
Posts: 555
Contact:

Unread post by sm_jamieson » 24 Oct 2017, 15:52

For a single line of CSV, the Usine "commatext" is just that. In a script you can read in a text file and convert to commatext using built-in pascal/Delphi functions. An example from one of my scripts is below. I'm not sure if you can do it just by wiring modules.
Of course you can do it in C++ in an SDK user module - I use a little CSV function I found, and PUGIXML for XML.

procedure ReadPresetFile(filename : ansiString; inst : integer);
var presetcomma : ansiString;
var presetsList, presetInfList : tstringlist;
var i:integer;
begin
// Read in a preset file and store it in an Instrument record.
// The preset file is a stringlist of commatext

Instruments[inst].filename := filename;

presetsList.create; // whole file - list of presets
presetInfList.create; // preset info being processed

presetsList.LoadFromFile(USINEBASEDIR + '' + PRESETMAPDIR + '' + filename);
setarraylength(Instruments[inst].Presets, presetsList.count);
Instruments[inst].Presetcount := presetsList.count;

for i := 0 to presetsList.count-1 do begin
presetcomma := presetsList.getStrings(i);
presetInfList.setCommaText(presetcomma);

Instruments[inst].Presets.number := StrToInt(presetInfList.getStrings(0));
Instruments[inst].Presets.bank := StrToInt(presetInfList.getStrings(1));
Instruments[inst].Presets.prog := StrToInt(presetInfList.getStrings(2));
Instruments[inst].Presets.name := presetInfList.getStrings(3);
Instruments[inst].Presets.category := presetInfList.getStrings(4);
StoreCategory(Instruments[inst].Categories, Instruments[inst].Presets.category);

presetInfList.clear();
end;

presetsList.free;
presetInfList.free;
end;

Ant1
New member
Posts: 4
Contact:

Unread post by Ant1 » 24 Oct 2017, 16:28

Thanks 23fx23 ! Do you by any chance know where to find more info on how text file should/can be formatted ? Thx in advance.

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 24 Oct 2017, 17:38

Not sure what you need, but here an patch example with basic manipulation of a csv
place the csv file in your C:
Download

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 24 Oct 2017, 17:39

it depends a bit of what you want, the simplest way is having a simple text file where each line got a value, ie
1
2
3
...
then i suppose you want convert number values so this script for exemple will read each line, convert text to float and put in an array:

Code: Select all

//////////////////////////
// Read text File lines and outputs float array
/////////////////////////
// declaration
const filename = 'c:list.txt';  
var pArray : Tparameter;   
var st   : TStringList;                  
//////////////////////////////
procedure destroy;  
begin
st.free;
end                                      
// initialisation : create parameters      
procedure init;
var i : integer;
begin 
    
  pArray := CreateParam('Array_out',ptArray);  
  setIsInput(pArray,false);
  //////////////////////////////////////////////////////
  st.create;
  st.clear;               
  st.loadfromFile(filename);
  setLength(pArray,st.count);
  for i := 0 to st.count-1
  do begin
    setDataArrayValue(pArray,i,strToFloat(st.getstrings(i)));
  end;
  st.free; 
end;
//////////////////////////////////
if you can't get a line by line text easily, but got values separated by commas ie 1,2,3,4,5,6, ect on a single line can use this one:

Code: Select all

//////////////////////////
// Read text File lines and outputs float array
/////////////////////////
// declaration
const filename = 'c:list.txt';  
var pArray : Tparameter;
var stA : TStringList;
var st   : TStringList;                  
//////////////////////////////
procedure destroy;  
begin
st.free;   
stA.free; 
end                                      
// initialisation : create parameters      
procedure init;
var i : integer;
begin 
    
  pArray := CreateParam('Array_out',ptArray);  
  setIsInput(pArray,false);
  //////////////////////////////////////////////////////
  st.create;
  st.clear;   
  stA.create;
  stA.clear;  
 ///////////////////////////////////
  stA.loadfromFile(filename);          
  St.SetCommaText(stA.getstrings(0));   
  setLength(pArray,st.count);
  for i := 0 to st.count-1
  do begin
    setDataArrayValue(pArray,i,strToFloat(st.getstrings(i)));
  end;
  st.free; 
end;
Edit : cross post hadn't see nay's one, might work out of the box ;)

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 24 Oct 2017, 20:22

And for fun a more ergonomic and musical one
of course works with simple csv , one colonne..( same csv file as before)
Download

Ant1
New member
Posts: 4
Contact:

Unread post by Ant1 » 25 Oct 2017, 17:06

Merci à tous ! J'explore tout çà.

A

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests