从Chuck中的文件读取整数

pat*_*ick 6 audio chuck

我有这个ChucK代码:

"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;
Run Code Online (Sandbox Code Playgroud)

文本文件包含:

10
12
Run Code Online (Sandbox Code Playgroud)

(它每分钟都用python更新)

我想将速度和方向转换为int(或更好的浮点数).

我怎样才能做到这一点?

Owe*_* S. 6

使用atoiatofStd库中.假设您要将0-127(MIDI速度)转换为0到1.0之间的浮点数(对于单位生成器来说更方便):

Std.atoi(fio.readLine()) => int midi_velocity;
midi_velocity/127.0 => float velocity;
<<< velocity >>>;
Run Code Online (Sandbox Code Playgroud)

应打印0.078740 :(float)输入10.

或者如果你想直接浮动:

Std.atof(fio.readLine()) => float velocity;
<<< velocity >>>;
Run Code Online (Sandbox Code Playgroud)

打印10.000000 :(float).