Ticket #6279: softvol-trunk-5.patch

File softvol-trunk-5.patch, 11.1 KB (added by foobum@…, 12 years ago)

against r20844

  • mythtv/libs/libmyth/audiooutputalsa.cpp

    diff --git a/mythtv/libs/libmyth/audiooutputalsa.cpp b/mythtv/libs/libmyth/audiooutputalsa.cpp
    index 788ae18..8ae7419 100644
    a b void AudioOutputALSA::SetupMixer(void) 
    752752    if (mixer_handle != NULL)
    753753        CloseMixer();
    754754
     755    if (alsadevice.toLower() == "software")
     756        return;
     757
    755758    VERBOSE(VB_AUDIO, QString("Opening mixer %1").arg(device));
    756759
    757760    // 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 63f90a4..6b2d085 100644
    a b  
    11// Std C headers
    22#include <cmath>
     3#include <limits>
    34
    45// POSIX headers
    56#include <unistd.h>
    AudioOutputBase::AudioOutputBase(const AudioSettings &settings) : 
    5354    needs_upmix(false),
    5455    surround_mode(FreeSurround::SurroundModePassive),
    5556    old_audio_stretchfactor(1.0),
     57    volume(80),
    5658
    5759    blocking(false),
    5860
    void AudioOutputBase::Reconfigure(const AudioSettings &orig_settings) 
    322324    audio_bytes_per_sample = audio_channels * audio_bits / 8;
    323325    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
    324326
    325    
    326327    VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2(%3) sr %4")
    327328            .arg(audio_main_device).arg(audio_channels)
    328329            .arg(source_audio_channels).arg(audio_samplerate));
    void AudioOutputBase::Reconfigure(const AudioSettings &orig_settings) 
    336337        VERBOSE(VB_AUDIO, "Aborting reconfigure");
    337338        return;
    338339    }
    339 
     340   
     341    // Only used for software volume
     342    if (set_initial_vol && internal_vol)
     343        volume = gContext->GetNumSetting("PCMMixerVolume", 80);
     344   
    340345    SyncVolume();
    341346
    342347    VERBOSE(VB_AUDIO, LOC + QString("Audio fragment size: %1")
    int AudioOutputBase::GetAudioBufferedTime(void) 
    627632     return audbuf_timecode - GetAudiotime();
    628633}
    629634
     635void AudioOutputBase::SetSWVolume(int new_volume, bool save)
     636{
     637    volume = new_volume;
     638    if (save)
     639    {
     640        QString controlLabel = gContext->GetSetting("MixerControl", "PCM");
     641        controlLabel += "MixerVolume";
     642        gContext->SaveSetting(controlLabel, volume);   
     643    }
     644}
     645
     646int AudioOutputBase::GetSWVolume()
     647{
     648    return volume;
     649}
     650
     651template <class AudioDataType>
     652void AudioOutputBase::_AdjustVolume(AudioDataType *buffer, int len, bool music)
     653{
     654    float g = volume / 100.0;
     655
     656    // Should probably be exponential - this'll do
     657    g *= g;
     658   
     659    // Add gain to AC-3 - try to ~ match PCM volume
     660    if (audio_enc && audio_reenc)
     661        g *= 1.8;
     662
     663    // Music is relatively loud - ditto
     664    else if (music)
     665        g *= 0.4;
     666
     667    if (g == 1.0)
     668        return;
     669
     670    for (int i = 0; i < (int)(len / sizeof(AudioDataType)); i++)
     671    {
     672        float s = static_cast<float>(buffer[i]) * g /
     673                  static_cast<float>(numeric_limits<AudioDataType>::max());
     674        if (s >= 1.0)
     675            buffer[i] = numeric_limits<AudioDataType>::max();
     676        else if (s <= -1.0)
     677            buffer[i] = numeric_limits<AudioDataType>::min();
     678        else
     679            buffer[i] = static_cast<AudioDataType>
     680                        (s * numeric_limits<AudioDataType>::max());
     681    }
     682}
     683
     684void AudioOutputBase::AdjustVolume(void *buffer, int len, bool music) {
     685
     686    if (audio_bits == 8)
     687        _AdjustVolume<char>((char *)buffer, len, music);
     688    else if (audio_bits == 16)
     689        _AdjustVolume<short>((short *)buffer, len, music);
     690
     691}
     692
     693
    630694bool AudioOutputBase::AddSamples(char *buffers[], int samples,
    631695                                 long long timecode)
    632696{
    void AudioOutputBase::_AddSamples(void *buffer, bool interleaved, int samples, 
    9701034       
    9711035    }
    9721036
     1037    if (internal_vol && SWVolume())
     1038    {
     1039        int bdiff = kAudioRingBufferSize - waud;
     1040        bool music = (timecode < 1);
     1041
     1042        if (bdiff < len)
     1043        {
     1044            AdjustVolume(audiobuffer + waud, bdiff, music);
     1045            AdjustVolume(audiobuffer, len - bdiff, music);
     1046        }
     1047        else
     1048            AdjustVolume(audiobuffer + waud, len, music);
     1049    }
     1050
    9731051    // Encode to AC-3?
    9741052    if (encoder)
    9751053    {
  • mythtv/libs/libmyth/audiooutputbase.h

    diff --git a/mythtv/libs/libmyth/audiooutputbase.h b/mythtv/libs/libmyth/audiooutputbase.h
    index 4bcaaa4..da4b0d7 100644
    a b class AudioOutputBase : public AudioOutput, public QThread 
    4747
    4848    virtual void Reset(void);
    4949
     50    void SetSWVolume(int new_volume, bool save);
     51    int GetSWVolume(void);
     52
    5053    // timecode is in milliseconds.
    5154    virtual bool AddSamples(char *buffer, int samples, long long timecode);
    5255    virtual bool AddSamples(char *buffers[], int samples, long long timecode);
    class AudioOutputBase : public AudioOutput, public QThread 
    153156    int src_quality;
    154157
    155158 private:
     159    // software volume
     160    template <class AudioDataType>
     161    void _AdjustVolume(AudioDataType *buffer, int len, bool music);
     162    void AdjustVolume(void *buffer, int len, bool music);
     163   
    156164    // resampler
    157165    bool need_resampler;
    158166    SRC_STATE *src_ctx;
    class AudioOutputBase : public AudioOutput, public QThread 
    169177    int surround_mode;
    170178    bool allow_ac3_passthru;
    171179    float old_audio_stretchfactor;
     180    int volume;
    172181
    173182    bool blocking; // do AddSamples calls block?
    174183
  • mythtv/libs/libmyth/audiooutputoss.cpp

    diff --git a/mythtv/libs/libmyth/audiooutputoss.cpp b/mythtv/libs/libmyth/audiooutputoss.cpp
    index 8d3b135..69f9f02 100644
    a b void AudioOutputOSS::VolumeInit() 
    313313    int volume = 0;
    314314
    315315    QString device = gContext->GetSetting("MixerDevice", "/dev/mixer");
     316    if (device.toLower() == "software")
     317        return;
    316318    QByteArray dev = device.toAscii();
    317319    mixerfd = open(dev.constData(), O_RDONLY);
    318320
  • mythtv/libs/libmyth/volumebase.cpp

    diff --git a/mythtv/libs/libmyth/volumebase.cpp b/mythtv/libs/libmyth/volumebase.cpp
    index d1f1fb0..a6d053a 100644
    a b VolumeBase::VolumeBase() : 
    1010    internal_vol(false), volume(80),
    1111    current_mute_state(kMuteOff)
    1212{
     13    swvol = swvol_setting =
     14      (gContext->GetSetting("MixerDevice", "default").toLower() == "software");
     15}
     16
     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;
    1327}
    1428
    1529uint VolumeBase::GetCurrentVolume(void) const
    MuteState VolumeBase::NextMuteState(MuteState cur) 
    7690void VolumeBase::UpdateVolume(void)
    7791{
    7892    int new_volume = volume;
     93    bool save = true;
    7994    if (current_mute_state == kMuteAll)
    8095    {
    8196        new_volume = 0;
     97        save = false;
     98    }
     99
     100    if (swvol)
     101    {
     102        SetSWVolume(new_volume, save);
     103        return;
    82104    }
    83105   
    84106    // TODO: Avoid assumption that there are 2 channels!
    void VolumeBase::UpdateVolume(void) 
    102124void VolumeBase::SyncVolume(void)
    103125{
    104126    // Read the volume from the audio driver and setup our internal state to match
    105     volume = GetVolumeChannel(0);
     127    if (swvol)
     128        volume = GetSWVolume();
     129    else
     130        volume = GetVolumeChannel(0);
    106131}
    107132
  • mythtv/libs/libmyth/volumebase.h

    diff --git a/mythtv/libs/libmyth/volumebase.h b/mythtv/libs/libmyth/volumebase.h
    index 4257f0b..68fb6f1 100644
    a b class MPUBLIC VolumeBase 
    2020    VolumeBase();   
    2121    virtual ~VolumeBase() {};
    2222
     23    void SWVolume(bool set);
     24    bool SWVolume(void);
    2325    virtual uint GetCurrentVolume(void) const;
    2426    virtual void SetCurrentVolume(int value);
    2527    virtual void AdjustCurrentVolume(int change);
    class MPUBLIC VolumeBase 
    3436
    3537    virtual int GetVolumeChannel(int channel) const = 0; // Returns 0-100
    3638    virtual void SetVolumeChannel(int channel, int volume) = 0; // range 0-100 for vol
     39    virtual void SetSWVolume(int new_volume, bool save) = 0;
     40    virtual int GetSWVolume(void) = 0;
    3741
    3842    void UpdateVolume(void);
    3943    void SyncVolume(void);
    class MPUBLIC VolumeBase 
    4448   
    4549    int volume;
    4650    MuteState current_mute_state;
     51    bool swvol;
     52    bool swvol_setting;
    4753
    4854};
    4955
  • mythtv/libs/libmythtv/avformatdecoder.cpp

    diff --git a/mythtv/libs/libmythtv/avformatdecoder.cpp b/mythtv/libs/libmythtv/avformatdecoder.cpp
    index 4530bc4..dec51e3 100644
    a b AvFormatDecoder::AvFormatDecoder(NuppelVideoPlayer *parent, 
    443443      // Audio
    444444      audioSamples(NULL),
    445445      allow_ac3_passthru(false),    allow_dts_passthru(false),
     446      internal_vol(false),
    446447      disable_passthru(false),      max_channels(2),
    447448      last_ac3_channels(0),         dummy_frame(NULL),
    448449      // DVD
    AvFormatDecoder::AvFormatDecoder(NuppelVideoPlayer *parent, 
    464465
    465466    allow_ac3_passthru = gContext->GetNumSetting("AC3PassThru", false);
    466467    allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false);
     468    internal_vol = gContext->GetNumSetting("MythControlsVolume", 0);
    467469    max_channels = (uint) gContext->GetNumSetting("MaxChannels", 2);
    468470
    469471    audioIn.sample_size = -32; // force SetupAudioStream to run once
    bool AvFormatDecoder::DoPassThrough(const AVCodecContext *ctx) 
    42044206
    42054207    if (ctx->codec_id == CODEC_ID_AC3)
    42064208        passthru = allow_ac3_passthru &&
    4207                    ctx->channels >= (int)max_channels;
     4209                   ctx->channels >= (int)max_channels &&
     4210                   !internal_vol;
    42084211    else if (ctx->codec_id == CODEC_ID_DTS)
    4209         passthru = allow_dts_passthru;
     4212        passthru = allow_dts_passthru && !internal_vol;
    42104213   
    42114214    passthru &= !transcoding && !disable_passthru;
    42124215    // 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 ea1fb23..4fe8d65 100644
    a b class AvFormatDecoder : public DecoderBase 
    269269    short int        *audioSamples;
    270270    bool              allow_ac3_passthru;
    271271    bool              allow_dts_passthru;
     272    bool              internal_vol;
    272273    bool              disable_passthru;
    273274    uint              max_channels;
    274275    uint              last_ac3_channels;
  • mythtv/programs/mythtranscode/transcode.cpp

    diff --git a/mythtv/programs/mythtranscode/transcode.cpp b/mythtv/programs/mythtranscode/transcode.cpp
    index 9958473..77ca720 100644
    a b class AudioReencodeBuffer : public AudioOutput 
    227227        // Do nothing
    228228        return false;
    229229    }
     230    virtual void SetSWVolume(int new_volume, bool save)
     231    {
     232        // Do nothing
     233        return;
     234    }
     235   
     236    virtual int GetSWVolume(void)
     237    {
     238        // Do nothing
     239        return 100;
     240    }
    230241
    231242    //  These are pure virtual in AudioOutput, but we don't need them here
    232243    virtual void bufferOutputData(bool){ return; }