Reading ints from a file in Chuck
Asked Answered
N

1

6

I have this ChucK code:

"examples/vento.txt" => string filename;
FileIO fio;

// open a file
fio.open(filename, FileIO.READ);

// ensure it's ok
if(!fio.good()) {
    cherr <= "can't open file: " <= filename <= " for reading..." <= IO.newline();
    me.exit();
}

fio.readLine() => string velocity;

fio.readLine() => string direction;

The text file contains:

10
12

(it's updated with python every minute)

I want to convert velocity and direction to int (or better float).

How can I do this?

Natation answered 15/7, 2010 at 18:46 Comment(0)
U
6

Use atoi and atof in the Std library. Let's say you want to translate from 0-127 (MIDI velocity) to a float between 0 and 1.0 (much more convenient for unit generators):

Std.atoi(fio.readLine()) => int midi_velocity;
midi_velocity/127.0 => float velocity;
<<< velocity >>>;

should print 0.078740 :(float) for an input of 10.

Or if you want to just go straight to float:

Std.atof(fio.readLine()) => float velocity;
<<< velocity >>>;

which prints 10.000000 :(float).

Untruth answered 16/7, 2010 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.