The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

revolooshon (2) [Avatar] Offline
#1
OK I'll preface this post by providing the the excuse that i'm a nOOb just in case:

the issue is with the overloaded pitch method example - set pitch by noteName.

the function asks for a string noteName parameter:

fun float pitch(string noteName)

the function then uses the charAt method to get the ASCI value of first element of the noteName string, subtract 65 from it and declare it as an integer data type variable base.

noteName.charAt(0) - 65 => int base;

OK here's the problem - a little farther on the exact same method that was used to derive the ASCI integer value of a charachter in the string noteName ie. charAt(),
is being used to conduct a string comparison:

if (noteName.charAt(1) == 's') // or 's' counts for sharp

here's the console error:

[listing92b_Simple_class.ck]:line(6smilie: cannot resolve operator '==' on types 'int' and 'string'

I created a simple test for the .charAt() method like this:

"A" => string mystr;

mystr.charAt(0) => int char_num;

<<< "the value of mystr.charAt(0) is:", mystr.charAt(0) >>>;
<<< "the value of int char_num is:", char_num >>>;

to which the console returned:

the value of mystr.charAt(0) is: 65
the value of int char_num is: 65

It does seem as if, yes, we are trying to compare a string with an integer when
the example in the book asks:

if (noteName.charAt(1) == 's')

So, is this their an error in the example code ?

I don't know enough about all the string methods in chuck to resolve the problem myself or else i would.
perry.cook (29) [Avatar] Offline
#2
Re: Listing 9.3 Add These Overloaded pitch() Functions to Simple
HumÂ… This is interesting. The

.charAt(int) method returns an integer ASCII code,
not a string. So that line should work perfectly fine,
because 's' (with the single apostrophes, not quotes)
is also an integer.

First check to make sure you're not using quotes,
because "s" is a string and would break the
comparison in the way you describe.

2nd, tell us what OS you're using (OSX10.?, Windoze
7.what, 8.what, XP, whatever, LINUX which version)
and what version of chuck/mini (About in the miniAudicle
menu, or chuck --h for the command line).

3rd copy and paste your whole program here so we
can all check it.

We'll figure it out, keep ChucKing!!

PRC
revolooshon (2) [Avatar] Offline
#3
Re: Listing 9.3 Add These Overloaded pitch() Functions to Simple
perry - thanks.

the short story is i followed your instruction regarding the single/double quotes
on the comparison and it works:

this is how i tested:

// note numbers A thru G
[21, 23, 12, 14, 16, 17, 19] @=> int notes[];

"A#3" => string noteName;

noteName.charAt(0) - 65 => int base;

<<< "1) noteName:", noteName >>>;
<<< "2) noteName.charAt(0):", noteName.charAt(0) >>>;
<<< "3) base:", base >>>;
<<< "4) notes array indexed by base:", "notes:[" + base + "]" >>>;

notes[base] => int note;

<<< "test second character in noteName for sharp or flat" >>>;
<<< "5) noteName.charAt(1): ", noteName.charAt(1) >>>;
<<< "6) noteName.charAt(1) == '#':", noteName.charAt(1) == '#' >>>;

// test for the ASCI val octave input in the noteName string

noteName.charAt(noteName.length() - 1) - 48 => int oct;

<<< "7) noteName.length() - 1:", noteName.length() - 1 >>>;
<<< "smilie noteName.charAt(noteName.length() - 1):", noteName.charAt(noteName.length() - 1) >>>;
<<< "9) noteName.charAt(noteName.length() - 1) - 48:", noteName.charAt(noteName.length() - 1) - 48 >>>;
<<< "10) oct:", oct >>>;

console ouput:

1) noteName: A#3
2) noteName.charAt(0): 65
3) base: 0
4) notes array indexed by base: notes:[0]
5) noteName.charAt(1): 35
6) noteName.charAt(1) == '#': 1
7) noteName.length() - 1: 2
smilie noteName.charAt(noteName.length() - 1): 51
9) noteName.charAt(noteName.length() - 1) - 48: 3
10) oct: 3

however the pitch by noteName method in the example (listing 9.3) throws an error
and this is from a straight cut and paste out of the book code:

(overloaded pitch methods by MIDI and float worked)

// listing93 overloaded pitch function using noteName
// changed class from public to private for this response

// simple example of a resonant pop sound making class
private class Simple
{
// our Simple instrument patch
Impulse imp => ResonZ filt => dac;

// some default settings
100.0 => filt.Q => filt.gain;
1000.0 => filt.freq;

// set freq as we would any instrument
function void freq(float freq)
{
freq => filt.freq;
}

// method to allow setting Q
function void setQ(float Q)
{
Q => filt.Q;
}

// method to allow setting gain
function void setGain(float gain)
{
filt.Q() * gain => imp.gain;
}

// play a note by firing impulse
function void noteOn(float volume)
{
volume => imp.next;
}


// Needs specific format: C4, D#5, Eb3, As3, Bf6
fun float pitch(string noteName)
{

[21,23,12,14,16,17,19] @=> int notes[]; // Note numbers A thru G

noteName.charAt(0) - 65 => int base;

notes[base] => int note;

0.0 => float freq;

if(base > - 1 && base < 7)
{
if (noteName.charAt(1) == '#')
notes[base] + 1 => note;

if (noteName.charAt(1) == 's')
notes[base] + 1 => note;

if (noteName.charAt(1) == 'b')
notes[base] - 1 => note;

if (noteName.charAt(1) == 'f')
notes[base] - 1 => note;
}

else
{
<<< "Illegal Note Name!!" >>>;
return 0.0;
}

noteName.charAt(noteName.length() - 1) - 48 => int oct;

if (oct > -1 && oct < 10)
{
note + 12 * oct => note;
return this.pitch(note);
}

else
{
<<< "Illegal Octave!!" >>>;
return 0.0;
}

}

}

// test noteName
Simple s;

// noteName note
s.pitch("G#5");
1 => s.noteOn;
second => now;

console:
[overloaded_noteName_to_pitch.ck]:line(76): ... for function 'Simple.pitch(...)' ...
[overloaded_noteName_to_pitch.ck]:line(76): ...(please check the argument types)
[overloaded_noteName_to_pitch.ck]: ...in function 'pitch'


It's not the end of the world that it's throwing this error but I'll be interested to see if anyone else posts an encounter of the same issue in future.

System:
MBPro
OSX version 10.9.2
Processor 2.7 GHz Intel Core i7
Memory 16 GB 1600 MHz DDR3

MiniAudicle:
version 1.3.0 (gidora)
Copyright (c) Spencer Salazar

ChucK: version 1.3.2.0 (chimera) 64-bit
Copyright (c) Ge Wang and Perry Cook
http://chuck.cs.princeton.edu/

PS. I'm using this version of miniaudicle and chuck due to experiencing the rendering issue with the double quotes
in the latest version on my system.

ie. ASCI DEC 34


I hope this assists - john