Ticket #8341: output-freq.2.diff

File output-freq.2.diff, 11.7 KB (added by eriktorbjorn, 20 years ago)

Updated patch against a May 25 CVS snapshot

  • README

    diff -urN ScummVM/README ScummVM+test/README
    old new  
    3434 * 7.3 Native MIDI support
    3535 * 7.4 UNIX native & ALSA sequencer support
    3636 * 7.5 Using compressed audiofiles (MP3, Ogg Vorbis, Flac)
     37 * 7.6 Output sample rate
    37388.0) Configuration Files
    38399.0) Compiling
    3940X.X) Credits
     
    330331                           atari, fmtowns, mac, pc)
    331332  --multi-midi             Enable combination of Adlib and native MIDI
    332333  --native-mt32            True Roland MT-32 (disable GM emulation)
     334  --output-rate=RATE       Select output sample rate in Hz (e.g. 22050)
    333335  --aspect-ratio           Enable aspect ratio correction
    334336
    335337  --alt-intro              Use alternative intro for CD versions of Beneath a
     
    861863Eventually you will have a much smaller *.mp3, *.ogg or *.fla file, copy this
    862864file to your game dir. You can safely remove the old file.
    863865
     8667.6) Output sample rate:
     867---- -------------------
     868
     869The output sample rate tells ScummVM how many sound samples to play per channel
     870per second. There is much that could be said on this subject, but most of it
     871would be irrelevant here. The short version is that for most games 22050 Hz is
     872fine, but in some cases 44100 Hz is preferable. On extremely low-end systems
     873you may want to use 11025 Hz, but it's unlikely that you have to worry about
     874that.
     875
     876To elaborate, most of the sounds ScummVM has to play were sampled at either
     87722050 Hz or 11025 Hz. Using a higher sample rate will not magically improve the
     878quality of these sounds. Hence, 22050 Hz is fine.
     879
     880Some games use CD audio. If you use compressed files for this, they are
     881probably sampled at 44100 Hz, so for these games that may be a better choice of
     882sample rate.
     883
     884When using the Adlib, FM Towns, PC Speaker or IBM PCjr music drivers, ScummVM
     885is responsible for generating the samples. Usually 22050 Hz will be plenty for
     886these, but there is at least one piece of Adlib music in Beneath a Steeel Sky
     887that will sound a lot better at 44100 Hz.
     888
     889Using frequencies in between is not recommended. For one thing, your sound card
     890may not support it. In theory, ScummVM should fall back on a sensible frequency
     891in that case, but don't count on it. More importantly, ScummVM has to resample
     892all sounds to its output frequency. This is much easier to do well if the
     893output frequency is a multiple of the original frequency.
     894
    864895
    8658968.0) Configuration file:
    866897---- -------------------
     
    944975        joystick_num    number   Number of joystick device to use for input
    945976        master_volume   number   The master volume setting (0-255)
    946977        music_driver    string   The music engine to use.
     978        output_rate     number   The output sample rate to use, in Hz. Sensible
     979                                 values are 11025, 22050 and 44100.
    947980        alsa_port       string   Port to use for output when using the
    948981                                 ALSA music driver.
    949982        music_volume    number   The music volume setting (0-255)
  • backends/sdl/sdl-common.h

    diff -urN ScummVM/backends/sdl/sdl-common.h ScummVM+test/backends/sdl/sdl-common.h
    old new  
    133133       
    134134        virtual void setWindowCaption(const char *caption);
    135135        virtual bool openCD(int drive);
     136        virtual void setOutputSampleRate(int rate);
    136137        virtual int getOutputSampleRate() const;
    137138
    138139        virtual bool hasFeature(Feature f);
     
    169170        SDL_Surface *_tmpscreen;
    170171        bool _overlayVisible;
    171172
     173        // Audio
     174        int _samplesPerSec;
     175
    172176        // CD Audio
    173177        SDL_CD *_cdrom;
    174178        int cd_track, cd_num_loops, cd_start_frame, cd_duration;
  • backends/sdl/sdl.cpp

    diff -urN ScummVM/backends/sdl/sdl.cpp ScummVM+test/backends/sdl/sdl.cpp
    old new  
    9595#endif
    9696        _hwscreen(0), _screen(0), _screenWidth(0), _screenHeight(0),
    9797        _tmpscreen(0), _overlayVisible(false),
     98        _samplesPerSec(SAMPLES_PER_SEC),
    9899        _cdrom(0), _scaler_proc(0), _modeChanged(false), _dirty_checksums(0),
    99100        _mouseVisible(false), _mouseDrawn(false), _mouseData(0),
    100101        _mouseHotspotX(0), _mouseHotspotY(0),
     
    289290
    290291bool OSystem_SDL::setSoundCallback(SoundProc proc, void *param) {
    291292        SDL_AudioSpec desired;
     293        SDL_AudioSpec obtained;
    292294
    293295        memset(&desired, 0, sizeof(desired));
    294296
    295         desired.freq = SAMPLES_PER_SEC;
     297        // Originally, we always used 2048 samples. This loop will produce the
     298        // same result at 22050 Hz, and should hopefully produce something
     299        // sensible for other frequencies. Note that it must be a power of two.
     300
     301        uint16 samples = 0x8000;
     302
     303        for (;;) {
     304                if (samples / (_samplesPerSec / 1000) < 100)
     305                        break;
     306                samples >>= 1;
     307        }
     308
     309        desired.freq = _samplesPerSec;
    296310        desired.format = AUDIO_S16SYS;
    297311        desired.channels = 2;
    298         desired.samples = 2048;
     312        desired.samples = samples;
    299313        desired.callback = proc;
    300314        desired.userdata = param;
    301         if (SDL_OpenAudio(&desired, NULL) != 0) {
     315        if (SDL_OpenAudio(&desired, &obtained) != 0) {
    302316                return false;
    303317        }
     318        // Note: This should be the obtained output rate, but it seems that at
     319        // least on some platforms SDL will lie and claim it did get the rate
     320        // even if it didn't. Probably only happens for "weird" rates, though.
     321        _samplesPerSec = obtained.freq;
    304322        SDL_PauseAudio(0);
    305323        return true;
    306324}
     
    309327        SDL_CloseAudio();
    310328}
    311329
     330void OSystem_SDL::setOutputSampleRate(int rate) {
     331        // The function can't be called after the audio device has been opened
     332        if (SDL_GetAudioStatus() == SDL_AUDIO_STOPPED)
     333                _samplesPerSec = rate;
     334}
     335
    312336int OSystem_SDL::getOutputSampleRate() const {
    313         return SAMPLES_PER_SEC;
     337        return _samplesPerSec;
    314338}
    315339
    316340#pragma mark -
  • base/gameDetector.cpp

    diff -urN ScummVM/base/gameDetector.cpp ScummVM+test/base/gameDetector.cpp
    old new  
    7878        "                           atari, fmtowns, mac, pc)\n"
    7979        "  --multi-midi             Enable combination Adlib and native MIDI\n"
    8080        "  --native-mt32            True Roland MT-32 (disable GM emulation)\n"
     81        "  --output-rate=RATE       Select output sample rate in Hz (e.g. 22050)\n"
    8182        "  --aspect-ratio           Enable aspect ratio correction\n"
    8283        "\n"
    8384#if !defined(DISABLE_SKY) || !defined(DISABLE_QUEEN)
     
    312313                                ConfMan.set("music_driver", option, kTransientDomain);
    313314                        END_OPTION
    314315
     316                        DO_LONG_OPTION("output-rate")
     317                                ConfMan.set("output_rate", (int)strtol(option, 0, 10), kTransientDomain);
     318                        END_OPTION
     319
    315320                        DO_OPTION_BOOL('f', "fullscreen")
    316321                                ConfMan.set("fullscreen", cmdValue, kTransientDomain);
    317322                        END_OPTION
  • common/system.h

    diff -urN ScummVM/common/system.h ScummVM+test/common/system.h
    old new  
    546546        virtual void clearSoundCallback() = 0;
    547547
    548548        /**
     549         * Set the desired output sample rate. It has to be called before
     550         * setSoundCallback(), and it's not certain that we actually get it.
     551         * Use getOutputSampleRate() to find out.
     552         */
     553        virtual void setOutputSampleRate(int rate) = 0;
     554
     555        /**
    549556         * Determine the output sample rate. Audio data provided by the sound
    550557         * callback will be played using this rate.
    551558         * @return the output sample rate
  • doc/05_01.tex

    diff -urN ScummVM/doc/05_01.tex ScummVM+test/doc/05_01.tex
    old new  
    3737                          &atari, fmtowns, mac, pc)\\
    3838  --multi-midi            &Enable combination of Adlib and native MIDI\\
    3939  --native-mt32           &True Roland MT-32 (disable GM emulation)\\
     40  --output-rate=RATE      &Select output sample rate in Hz (e.g. 22050)\\
    4041  --aspect-ratio          &Enable aspect ratio correction\\
    4142\\
    4243  --alt-intro             &Use alternative intro for CD versions of Beneath a\\
  • doc/07.tex

    diff -urN ScummVM/doc/07.tex ScummVM+test/doc/07.tex
    old new  
    3434\input {07_02.tex}
    3535\input {07_03.tex}
    3636\input {07_04.tex}
    37 \input {07_05.tex}
    38  No newline at end of file
     37\input {07_05.tex}
     38\input {07_06.tex}
  • doc/07_06.tex

    diff -urN ScummVM/doc/07_06.tex ScummVM+test/doc/07_06.tex
    old new  
     1
     2%%% Local Variables:
     3%%% mode: latex
     4%%% TeX-master: "readme"
     5%%% End:
     6
     7\subsection{Output sample rate}
     8
     9The output sample rate tells ScummVM how many sound samples to play per channel
     10per second. There is much that could be said on this subject, but most of it
     11would be irrelevant here. The short version is that for most games 22050 Hz is
     12fine, but in some cases 44100 Hz is preferable. On extremely low-end systems
     13you may want to use 11025 Hz, but it's unlikely that you have to worry about
     14that.
     15
     16To elaborate, most of the sounds ScummVM has to play were sampled at either
     1722050 Hz or 11025 Hz. Using a higher sample rate will not magically improve the
     18quality of these sounds. Hence, 22050 Hz is fine.
     19
     20Some games use CD audio. If you use compressed files for this, they are
     21probably sampled at 44100 Hz, so for these games that may be a better choice of
     22sample rate.
     23
     24When using the Adlib, FM Towns, PC Speaker or IBM PCjr music drivers, ScummVM
     25is responsible for generating the samples. Usually 22050 Hz will be plenty for
     26these, but there is at least one piece of Adlib music in Beneath a Steeel Sky
     27that will sound a lot better at 44100 Hz.
     28
     29Using frequencies in between is not recommended. For one thing, your sound card
     30may not support it. In theory, ScummVM should fall back on a sensible frequency
     31in that case, but don't count on it. More importantly, ScummVM has to resample
     32all sounds to its output frequency. This is much easier to do well if the
     33output frequency is a multiple of the original frequency.
  • doc/08.tex

    diff -urN ScummVM/doc/08.tex ScummVM+test/doc/08.tex
    old new  
    9292        joystick\_num   &number   Number of joystick device to use for input\\
    9393        master\_volume  &number   The master volume setting (0-255)\\
    9494        music\_driver   &string   The music engine to use.\\
     95        output\_rate    &number   The output sample rate to use, in Hz. Sensible\\
     96                        &         values are 11025, 22050 and 44100.\\
    9597        alsa\_port      &string   Port to use for output when using the\\
    9698                        &         ALSA music driver.\\
    9799        music\_volume   &number   The music volume setting (0-255)\\
  • sound/mixer.cpp

    diff -urN ScummVM/sound/mixer.cpp ScummVM+test/sound/mixer.cpp
    old new  
    2121 */
    2222
    2323#include "stdafx.h"
     24#include "common/config-manager.h"
    2425#include "common/file.h"
    2526#include "common/util.h"
    2627
     
    111112        _premixProc = 0;
    112113        int i = 0;
    113114
    114         _outputRate = (uint) _syst->getOutputSampleRate();
    115 
    116         if (_outputRate == 0)
    117                 error("OSystem returned invalid sample rate");
     115        if (ConfMan.hasKey("output_rate"))
     116                _syst->setOutputSampleRate(ConfMan.getInt("output_rate"));
    118117
    119118        _globalVolume = 0;
    120119        _musicVolume = 0;
     
    125124                _channels[i] = 0;
    126125
    127126        _mixerReady = _syst->setSoundCallback(mixCallback, this);
     127        _outputRate = (uint) _syst->getOutputSampleRate();
     128
     129        if (_outputRate == 0)
     130                error("OSystem returned invalid sample rate");
     131
     132        debug(1, "Output sample rate: %d Hz", _outputRate);
    128133}
    129134
    130135SoundMixer::~SoundMixer() {