fix WAVE autodetect to be more robust and not require ungetc()