Pwm pitch + volume control?

Hi everyone. I'm looking for a way to control volume of a pwm output while a melody is being played. I'd like to use a photoresistor hooked up to an analog input to control the volume. The example toneMelody is similar to what my code looks like except it's in void loop. I know a photoresistor between the speaker and ground would be one way of doing it, but I want full control from no volume to full volume. Another output to a transistor that controls the pwm might work too, but I'd like to do it without many components. Any help would be greatly appreciated.

Photo resistors usually have a wide range of resistance, if it got up in resistance much at all you wouldn't get much volume being in series with a 4-8-16 ohm speaker. What would better is to have the photoresistor in series with an output feeding a self-amplified computer moniter speaker - got one of those around? The lower its resistance the higher the volume.

CrossRoads, thanks, but thats not really what im looking for. Space is limited so I would like to do it mostly in code. I'm leaning towards the using a transistor in series with the pwm output to the speaker, and then mapping a separate photoresistor input that outputs to the base of the transistor... though im not really sure how to work that out since I'm new to electronics and cant bias a transistor yet. I also saw this,36857.0.html, but it confuses me a bit and i'm not sure how i would use it. What I really want is to use all code: no transistor, no amplifier. Using light sensitivity to control the volume is also vital and it probably needs to be calibrated so i can get full control. Is that possible?

he has volume on there, and accurate pitch. you can also put in a lot of different waveforms besides his sine, by making more tables besides the sintable I based a long running project on it. just keep in mind when you reduce the volume in code you reduce the amount of bits resolution of the output.

also there is a change possible in the setup code for the pwm on pins 9 and 10 you change it to a 16 bit timer (but it only outputs 10 bit PWM) which is much better if you are changing the volume.

(i haven't adapted that to his code because i use a DAC, but i will look in to it soon)