"warning: narrowing conversion" error

Goodday,

I followed a tutorial on using the Arduino for making sound.

the github link: GitHub - dzlonline/the_synth: A simple to use 4 polyphonic wavetable synthesizer library for Arduino.

Im noodling around and did some cool stuff with Arduino's before, tho i never had this error and googling it led me to C++ pages which are out of my league atm.

The error that occurs is as following:

In file included from C:\Users\user\Documents\Arduino\libraries\the_synth/synth.h:12:0,

from C:\Users\user\Documents\Arduino\libraries\the_synth\examples\modulation\modulation.ino:16:

C:\Users\user\Documents\Arduino\libraries\the_synth/tables.h:1094:1: warning: narrowing conversion of '128' from 'int' to 'const char' inside { } [-Wnarrowing]

};

^

C:\Users\user\Documents\Arduino\libraries\the_synth/tables.h:1745:1: warning: narrowing conversion of '255' from 'int' to 'const char' inside { } [-Wnarrowing]

};

^

C:\Users\user\Documents\Arduino\libraries\the_synth/tables.h:1745:1: warning: narrowing conversion of '254' from 'int' to 'const char' inside { } [-Wnarrowing]

C:\Users\user\Documents\Arduino\libraries\the_synth/tables.h:1745:1: warning: narrowing conversion of '254' from 'int' to 'const char' inside { } [-Wnarrowing]

C:\Users\user\Documents\Arduino\libraries\the_synth/tables.h:1745:1: warning: narrowing conversion of '254' from 'int' to 'const char' inside { } [-Wnarrowing]

C:\Users\user\Documents\Arduino\libraries\the_synth/tables.h:1745:1: warning: narrowing conversion of '253' from 'int' to 'const char' inside { } [-Wnarrowing]

And the errors keeps on going, counting down, with skipping values, to a 133.

It seems to count down till 133 and it seems to skip some numbers. Since i did not write any of this code i have no idea where to look. I checked on the included libraries but i couldnt make anything of it...:frowning:

Many times this forum has helped without even having to ask because others had similar problems and solutions but this time i was not so lucky it seems.

Obviously it has some trouble with the library and narrowing conversion from "int" to "const char".

Is there someone that can see whats going on or what im doing wrong? The tutorial made it look easy :stuck_out_tongue:

People around here are generally unimpressed with the quality of code from Instructables.

A narrowing is when you try to assign a datatype such as an "int" to a smaller datatype like a "char".
Sometimes it is a problem at runtime, other times, you don't notice it. (which probably means you shouldn't have used "int" in the first place)

They are just warning messages so you can ignore them and see if the results work. Looks like the way the waveform table is initialized is producing the warnings. Values over 127 become negative numbers when saved in a 'char' array. You might be able to avoid the warnings by making the table 'unsigned char' (a.k.a. 'byte'). That may cause warnings, errors, or miscalculations elsewhere.

1 Like

Great thanks for the fast response. Yea instructables sometimes are great but more than often i end up making it myself or not at all cause of time.

Besides the errors, it compiles but there is no output what so ever. Is this then because of the shematic? i used an 10nF but didnt had a 10uF cap so used a 220uf and tried a 22uF aswell (those are the only ones around atm...) but no output. Or should i, using for instance the "any_hertz.ino", alter the library and change the ints?

I downloaded the library (GitHub - dzlonline/the_synth: A simple to use 4 polyphonic wavetable synthesizer library for Arduino.), unzipped, moved the 'the_synth' folder to my 'libraries' folder, re-started the IDE, opened File->Examples->the_synth->song and uploaded it to an Arduino UNO. Music plays (faintly) on a small speaker connected between Pin 11 and GND.

I ignored the 'narrowing conversion' warning messages but I did change the 'note<1000' to 'note<384' so it would not run off the end of the note or duration arrays. There was a compiler warning for that, too.

Edit: You can change 'melody' and 'noteDuration' tables from 'int' to 'byte' to reduce memory usage from 89% to 50%.

Edit 2: Changing the first element of tables.h:SawTable[] from 128 to 127 got rid of one of the warnings. Changing Env0[], Env1[], Env2[], and Env3[] from 'char' to 'unsigned char' got rid of the rest. Music still plays. Note that there are frequency tables at the top of table.h for 20 MHz and 16 MHz systems. For the UNO, comment out the 20 MHz tables and uncomment the 16 MHz tables. The music playes either way but the pitches will be off if you use the wrong table.