Ticket #6279: softvol-fixes-5.patch

File softvol-fixes-5.patch, 11.4 KB (added by foobum@…, 15 years ago)

against r20844

  • mythtv/libs/libmyth/audiooutputalsa.cpp

    diff --git a/mythtv/libs/libmyth/audiooutputalsa.cpp b/mythtv/libs/libmyth/audiooutputalsa.cpp
    index dd5bad9..02b74c5 100644
    a b void AudioOutputALSA::SetupMixer(void) 
    751751    if (mixer_handle != NULL)
    752752        CloseMixer();
    753753
     754    if (alsadevice.lower() == "software")
     755        return;
     756
    754757    VERBOSE(VB_AUDIO, QString("Opening mixer %1").arg(device));
    755758
    756759    // TODO: This is opening card 0. Fix for case of multiple soundcards
  • mythtv/libs/libmyth/audiooutputbase.cpp

    diff --git a/mythtv/libs/libmyth/audiooutputbase.cpp b/mythtv/libs/libmyth/audiooutputbase.cpp
    index 2cb54ea..20d2bac 100644
    a b AudioOutputBase::AudioOutputBase( 
    6161    needs_upmix(false),
    6262    surround_mode(FreeSurround::SurroundModePassive),
    6363    old_audio_stretchfactor(1.0),
     64    volume(80),
    6465
    6566    blocking(false),
    6667
    void AudioOutputBase::Reconfigure(int laudio_bits, int laudio_channels, 
    327328   
    328329    audio_bytes_per_sample = audio_channels * audio_bits / 8;
    329330    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
     331   
     332    if (internal_vol && audio_enc && audio_reenc)
     333    {
     334        VERBOSE(VB_AUDIO, LOC + "Using software vol control for AC-3");
     335        SWVolume(true);
     336    }
     337    else
     338        SWVolume(false);
    330339
    331340    VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2(%3) sr %4")
    332341            .arg(audio_main_device).arg(audio_channels)
    void AudioOutputBase::Reconfigure(int laudio_bits, int laudio_channels, 
    343352        VERBOSE(VB_AUDIO, "Aborting reconfigure");
    344353        return;
    345354    }
    346 
     355   
     356    // Only used for software volume
     357    if (set_initial_vol && internal_vol)
     358        volume = gContext->GetNumSetting("PCMMixerVolume", 80);
     359   
    347360    SyncVolume();
    348361   
    349362    VERBOSE(VB_AUDIO, LOC + QString("Audio fragment size: %1")
    void AudioOutputBase::SetAudiotime(void) 
    651664    pthread_mutex_unlock(&audio_buflock);
    652665}
    653666
     667void AudioOutputBase::SetSWVolume(int new_volume, bool save)
     668{
     669    volume = new_volume;
     670    if (save)
     671    {
     672        QString controlLabel = gContext->GetSetting("MixerControl", "PCM");
     673        controlLabel += "MixerVolume";
     674        gContext->SaveSetting(controlLabel, volume);   
     675    }
     676}
     677
     678int AudioOutputBase::GetSWVolume()
     679{
     680    return volume;
     681}
     682
     683template <class AudioDataType>
     684void AudioOutputBase::_AdjustVolume(AudioDataType *buffer, int len, bool music)
     685{
     686    float g = volume / 100.0;
     687
     688    // Should probably be exponential - this'll do
     689    g *= g;
     690   
     691    // Add gain to AC3 / DTS - try to ~ match PCM volume
     692    if (audio_codec == CODEC_ID_AC3 || audio_codec == CODEC_ID_DTS)
     693        g *= 1.8;
     694
     695    // Music is relatively loud - ditto
     696    else if (music)
     697        g *= 0.4;
     698
     699    if (g == 1.0)
     700        return;
     701
     702    for (int i = 0; i < (int)(len / sizeof(AudioDataType)); i++)
     703    {
     704        float s = static_cast<float>(buffer[i]) * g /
     705                  static_cast<float>(numeric_limits<AudioDataType>::max());
     706        if (s >= 1.0)
     707            buffer[i] = numeric_limits<AudioDataType>::max();
     708        else if (s <= -1.0)
     709            buffer[i] = numeric_limits<AudioDataType>::min();
     710        else
     711            buffer[i] = static_cast<AudioDataType>
     712                        (s * numeric_limits<AudioDataType>::max());
     713    }
     714}
     715
     716void AudioOutputBase::AdjustVolume(void *buffer, int len, bool music) {
     717
     718    if (audio_bits == 8)
     719        _AdjustVolume<char>((char *)buffer, len, music);
     720    else if (audio_bits == 16)
     721        _AdjustVolume<short>((short *)buffer, len, music);
     722
     723}
     724
    654725bool AudioOutputBase::AddSamples(char *buffers[], int samples,
    655726                                 long long timecode)
    656727{
    void AudioOutputBase::_AddSamples(void *buffer, bool interleaved, int samples, 
    9961067       
    9971068    }
    9981069
     1070    if (internal_vol && SWVolume())
     1071    {
     1072        int bdiff = AUDBUFSIZE - waud;
     1073        bool music = (timecode < 1);
     1074
     1075        if (bdiff < len)
     1076        {
     1077            AdjustVolume(audiobuffer + waud, bdiff, music);
     1078            AdjustVolume(audiobuffer, len - bdiff, music);
     1079        }
     1080        else
     1081            AdjustVolume(audiobuffer + waud, len, music);
     1082    }
     1083
    9991084    // Encode to AC-3?
    10001085    if (encoder)
    10011086    {
  • mythtv/libs/libmyth/audiooutputbase.h

    diff --git a/mythtv/libs/libmyth/audiooutputbase.h b/mythtv/libs/libmyth/audiooutputbase.h
    index bdf808c..acc2f43 100644
    a b class AudioOutputBase : public AudioOutput 
    6363
    6464    virtual void Reset(void);
    6565
     66    void SetSWVolume(int new_volume, bool save);
     67    int GetSWVolume(void);
     68
    6669    // timecode is in milliseconds.
    6770    virtual bool AddSamples(char *buffer, int samples, long long timecode);
    6871    virtual bool AddSamples(char *buffers[], int samples, long long timecode);
    class AudioOutputBase : public AudioOutput 
    118121    int audiofree(bool use_lock); // number of free bytes in audio buffer
    119122
    120123    void UpdateVolume(void);
    121    
     124
    122125    void SetStretchFactorLocked(float factor);
    123126
    124127    int GetBaseAudioTime()                    const { return audiotime;       }
    class AudioOutputBase : public AudioOutput 
    159162    int src_quality;
    160163
    161164 private:
     165    // software volume
     166    template <class AudioDataType>
     167    void _AdjustVolume(AudioDataType *buffer, int len, bool music);
     168    void AdjustVolume(void *buffer, int len, bool music);
     169   
    162170    // resampler
    163171    bool need_resampler;
    164172    SRC_STATE *src_ctx;
    class AudioOutputBase : public AudioOutput 
    179187    int surround_mode;
    180188    bool allow_ac3_passthru;
    181189    float old_audio_stretchfactor;
     190    int volume;
    182191
    183192    bool blocking; // do AddSamples calls block?
    184193
  • mythtv/libs/libmyth/audiooutputoss.cpp

    diff --git a/mythtv/libs/libmyth/audiooutputoss.cpp b/mythtv/libs/libmyth/audiooutputoss.cpp
    index 0f67c6a..2c98143 100644
    a b void AudioOutputOSS::VolumeInit() 
    319319    int volume = 0;
    320320
    321321    QString device = gContext->GetSetting("MixerDevice", "/dev/mixer");
     322    if (device.lower() == "software")
     323        return;
    322324    mixerfd = open(device.ascii(), O_RDONLY);
    323325
    324326    QString controlLabel = gContext->GetSetting("MixerControl", "PCM");
  • mythtv/libs/libmyth/volumebase.cpp

    diff --git a/mythtv/libs/libmyth/volumebase.cpp b/mythtv/libs/libmyth/volumebase.cpp
    index 6823bbc..2431ff2 100644
    a b VolumeBase::VolumeBase() 
    1010    volume = 80;
    1111    current_mute_state=MUTE_OFF;
    1212    internal_vol = false;
     13    swvol = swvol_setting =
     14        (gContext->GetSetting("MixerDevice", "default").lower() == "software");
    1315};
    1416
     17bool VolumeBase::SWVolume(void)
     18{
     19    return swvol;
     20}
     21
     22void VolumeBase::SWVolume(bool set)
     23{
     24    if (swvol_setting)
     25        return;
     26    swvol = set;
     27}
     28
     29
    1530int VolumeBase::GetCurrentVolume(void)
    1631{
    1732    return volume;
    kMuteState VolumeBase::IterateMutedChannels(void) 
    88103void VolumeBase::UpdateVolume(void)
    89104{
    90105    int new_volume = volume;
     106    bool save = true;
    91107    if (current_mute_state == MUTE_BOTH)
    92108    {
    93109        new_volume = 0;
     110        save = false;
     111    }
     112
     113    if (swvol)
     114    {
     115        SetSWVolume(new_volume, save);
     116        return;
    94117    }
    95118   
    96119    // TODO: Avoid assumption that there are 2 channels!
    void VolumeBase::UpdateVolume(void) 
    114137void VolumeBase::SyncVolume(void)
    115138{
    116139    // Read the volume from the audio driver and setup our internal state to match
    117     volume = GetVolumeChannel(0);
     140    if (swvol)
     141        volume = GetSWVolume();
     142    else
     143        volume = GetVolumeChannel(0);
    118144}
    119145
  • mythtv/libs/libmyth/volumebase.h

    diff --git a/mythtv/libs/libmyth/volumebase.h b/mythtv/libs/libmyth/volumebase.h
    index f41a5d7..0601568 100644
    a b class MPUBLIC VolumeBase 
    2020    VolumeBase();   
    2121    virtual ~VolumeBase() {};
    2222
     23    void SWVolume(bool set);
     24    bool SWVolume(void);
    2325    virtual int GetCurrentVolume(void);
    2426    virtual void SetCurrentVolume(int value);
    2527    virtual void AdjustCurrentVolume(int change);
    class MPUBLIC VolumeBase 
    3234
    3335    virtual int GetVolumeChannel(int channel) = 0; // Returns 0-100
    3436    virtual void SetVolumeChannel(int channel, int volume) = 0; // range 0-100 for vol
     37    virtual void SetSWVolume(int new_volume, bool save) = 0;
     38    virtual int GetSWVolume(void) = 0;
    3539
    3640    void UpdateVolume(void);
    3741    void SyncVolume(void);
    class MPUBLIC VolumeBase 
    4246   
    4347    int volume;
    4448    kMuteState current_mute_state;
     49    bool swvol;
     50    bool swvol_setting;
    4551
    4652};
    4753
  • mythtv/libs/libmythtv/avformatdecoder.cpp

    diff --git a/mythtv/libs/libmythtv/avformatdecoder.cpp b/mythtv/libs/libmythtv/avformatdecoder.cpp
    index 972f0fd..75a152b 100644
    a b AvFormatDecoder::AvFormatDecoder(NuppelVideoPlayer *parent, 
    410410      // Audio
    411411      audioSamples(new short int[AVCODEC_MAX_AUDIO_FRAME_SIZE]),
    412412      allow_ac3_passthru(false),    allow_dts_passthru(false),
     413      internal_vol(false),
    413414      disable_passthru(false),      max_channels(2),
    414415      last_ac3_channels(0),         dummy_frame(NULL),
    415416      // DVD
    AvFormatDecoder::AvFormatDecoder(NuppelVideoPlayer *parent, 
    428429
    429430    allow_ac3_passthru = gContext->GetNumSetting("AC3PassThru", false);
    430431    allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false);
     432    internal_vol = gContext->GetNumSetting("MythControlsVolume", 0);
    431433    max_channels = (uint) gContext->GetNumSetting("MaxChannels", 2);
    432434
    433435    audioIn.sample_size = -32; // force SetupAudioStream to run once
    bool AvFormatDecoder::DoPassThrough(const AVCodecContext *ctx) 
    39683970
    39693971    if (ctx->codec_id == CODEC_ID_AC3)
    39703972        passthru = allow_ac3_passthru &&
    3971                    ctx->channels >= (int)max_channels;
     3973                   ctx->channels >= (int)max_channels &&
     3974                   !internal_vol;
    39723975    else if (ctx->codec_id == CODEC_ID_DTS)
    3973         passthru = allow_dts_passthru;
     3976        passthru = allow_dts_passthru && !internal_vol;
    39743977   
    39753978    passthru &= !transcoding && !disable_passthru;
    39763979    // Don't know any cards that support spdif clocked at < 44100
  • mythtv/libs/libmythtv/avformatdecoder.h

    diff --git a/mythtv/libs/libmythtv/avformatdecoder.h b/mythtv/libs/libmythtv/avformatdecoder.h
    index f008136..482ffe2 100644
    a b class AvFormatDecoder : public DecoderBase 
    263263    short int        *audioSamples;
    264264    bool              allow_ac3_passthru;
    265265    bool              allow_dts_passthru;
     266    bool              internal_vol;
    266267    bool              disable_passthru;
    267268    uint              max_channels;
    268269    uint              last_ac3_channels;
  • mythtv/programs/mythtranscode/transcode.cpp

    diff --git a/mythtv/programs/mythtranscode/transcode.cpp b/mythtv/programs/mythtranscode/transcode.cpp
    index 653f9d4..64f6e8d 100644
    a b class AudioReencodeBuffer : public AudioOutput 
    222222        // Do nothing
    223223        return false;
    224224    }
     225    virtual void SetSWVolume(int new_volume, bool save)
     226    {
     227        // Do nothing
     228        return;
     229    }
     230   
     231    virtual int GetSWVolume(void)
     232    {
     233        // Do nothing
     234        return 100;
     235    }
    225236
    226237    //  These are pure virtual in AudioOutput, but we don't need them here
    227238    virtual void bufferOutputData(bool){ return; }