High pitched noise when playing wav files

Hey guys,

I'm a complete novice when it comes to designing circuits and stuff. I have a bcakground in computer science, with years of Java but no clue when it comes to electrical engineering. That being said, I am creating a project with my UNO in which I trigger switches to play corresponding .wav files. I have an SEEEDstudio SD card shield from RadioShack, and I have connected my 9th digital out to a 220 Ohm resistor, which is in turn hooked up to a 2N4401 transistor with 5v in on one side and out to my 8Ohm (.3W) speaker on the other, which is in turn grounded. My wav files play perfectly, albeit a bit on the quiet side, however as they play, there is a constant high pitched tone playing along with the music. Do you guys have any idea what I'm doing wrong? Or is this an un-fixable Arduino thing?

Here is my code:

#include <SD.h>                      // need to include the SD library
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10  //using digital pin 4 on arduino nano 328
#include <TMRpcm.h>           //  also need to include this library...

TMRpcm tmrpcm;   // create an object for use in this sketch
char mychar;
const int sw = 3;
int swst = 0;

void setup(){

  tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
  pinMode(3, INPUT);
  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
  tmrpcm.play("Wish.wav"); //the sound file "wish" will play each time the arduino powers up, or is reset
}

void loop(){  

  

}

The loop method is empty because I was first just trying to get the sound output right.

The high pitched noise is probably related to the PWM frequency, which can be reduced with a suitable low pass RC (or other type) filter. What does the author of the library say or recommend about its use?

I think that if the the author happened by the forum tonight, he would recommend something like using tmrpcm.quality(1); to oversample the audio at double rate, and mention that it defaults to 0 to now after a bunch of changes were made. Otherwise a low pass filter if the source is not the audio itelf. Power sources can be a source of noise also.

*edit to add: You can also raise and lower the volume a bit, and use complimentary output if wanted. The wiki at GitHub - TMRh20/TMRpcm: Arduino library for asynchronous playback of PCM/WAV files direct from SD card. Arduino Uno,Nano,Mega etc supported has more details on that stuff. I always just connect my little 8Ohm speakers directly, but most folks would recommend a resistor.