From 3ca8cd1bd2400fca4c473ff35ea7d4f52c215651 Mon Sep 17 00:00:00 2001
From: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
Date: Mon, 2 Jun 2008 14:08:33 -0400
Subject: [PATCH] Try to set pa volume

Failed for now
---
 src/audio/alsalayer.cpp   | 2 ++
 src/audio/alsalayer.h     | 5 +++++
 src/audio/audiolayer.h    | 2 ++
 src/audio/audiostream.cpp | 5 +++--
 src/audio/audiostream.h   | 6 +++---
 src/audio/pulselayer.cpp  | 9 +++++++++
 src/audio/pulselayer.h    | 6 ++++++
 7 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/src/audio/alsalayer.cpp b/src/audio/alsalayer.cpp
index c8d735969d..5d7e378837 100644
--- a/src/audio/alsalayer.cpp
+++ b/src/audio/alsalayer.cpp
@@ -658,3 +658,5 @@ AlsaLayer::adjustVolume( void* buffer , int len, int stream )
   }
   return src ; 
 }
+
+void AlsaLayer::setPlaybackVolume( double volume ){}
diff --git a/src/audio/alsalayer.h b/src/audio/alsalayer.h
index efa6125748..1b31eb2f03 100644
--- a/src/audio/alsalayer.h
+++ b/src/audio/alsalayer.h
@@ -201,6 +201,11 @@ class AlsaLayer : public AudioLayer {
      */
     void restorePulseAppsVolume( void ); 
 
+    /**
+     * UNUSED in ALSA layer
+     */
+    void setPlaybackVolume( double volume );
+
   private:
 
     /**
diff --git a/src/audio/audiolayer.h b/src/audio/audiolayer.h
index f162ff6e50..6e2b70879d 100644
--- a/src/audio/audiolayer.h
+++ b/src/audio/audiolayer.h
@@ -185,6 +185,8 @@ class AudioLayer {
 
     virtual void reducePulseAppsVolume( void ) = 0;
     virtual void restorePulseAppsVolume( void ) = 0;
+
+    virtual void setPlaybackVolume( double volume ) = 0;
   
     /**
      * Write accessor to the error state
diff --git a/src/audio/audiostream.cpp b/src/audio/audiostream.cpp
index a9f6e7345a..8b88009346 100644
--- a/src/audio/audiostream.cpp
+++ b/src/audio/audiostream.cpp
@@ -30,7 +30,7 @@ AudioStream::AudioStream( pa_context* context, int type, std::string desc, doubl
   sample_spec.channels = 1; 
   channel_map.channels = 1; 
   flag = PA_STREAM_AUTO_TIMING_UPDATE ;
-  _volume = PA_VOLUME_NORM * vol / 100 ;
+  pa_cvolume_set( &_volume , 1 , PA_VOLUME_MUTED ) ; // * vol / 100 ;
   
   _audiostream =  createStream( context );
 } 
@@ -87,7 +87,8 @@ AudioStream::createStream( pa_context* c )
   if( _streamType == PLAYBACK_STREAM ){
     pa_stream_connect_playback( s , NULL , NULL , 
 				PA_STREAM_INTERPOLATE_TIMING,
-				pa_cvolume_set(&cv, sample_spec.channels , _volume) , NULL );
+				&_volume, NULL);
+				//pa_cvolume_set(&cv, sample_spec.channels , _volume) , NULL );
   }
   else if( _streamType == CAPTURE_STREAM ){
     pa_stream_connect_record( s , NULL , NULL , PA_STREAM_START_CORKED );
diff --git a/src/audio/audiostream.h b/src/audio/audiostream.h
index 16bab21ec4..8327805e98 100644
--- a/src/audio/audiostream.h
+++ b/src/audio/audiostream.h
@@ -85,8 +85,8 @@ class AudioStream {
      */
     std::string getStreamName( void ) { return _streamDescription; }
 
-    void setVolume( double pc );
-    pa_volume_t getVolume( void ) { return _volume; }
+    void setVolume( double pc ) { _volume.values[0] *= pc/100; }
+    pa_cvolume getVolume( void ) { return _volume; }
 
   private:
     /**
@@ -131,7 +131,7 @@ class AudioStream {
      */
     pa_stream_flags_t flag;
     pa_sample_spec sample_spec ;
-    pa_volume_t _volume;
+    pa_cvolume _volume;
 
 };
 
diff --git a/src/audio/pulselayer.cpp b/src/audio/pulselayer.cpp
index a34f1f99c5..862d74fbaf 100644
--- a/src/audio/pulselayer.cpp
+++ b/src/audio/pulselayer.cpp
@@ -420,4 +420,13 @@ PulseLayer::restoreAppVolume( int index, int channels )
   pa_context_set_sink_input_volume( context, index, &volume, on_success, this) ;
 }
 
+void
+PulseLayer::setPlaybackVolume( double volume )
+{
+  //value between 0 and 100
+  AudioStream* s = getPlaybackStream();
+  s->setVolume( volume ); 
+}
+
+
 
diff --git a/src/audio/pulselayer.h b/src/audio/pulselayer.h
index 17fb909007..6265351f2e 100644
--- a/src/audio/pulselayer.h
+++ b/src/audio/pulselayer.h
@@ -165,6 +165,12 @@ class PulseLayer : public AudioLayer {
      */
     AudioStream* getRecordStream(){ return record;}
 
+    /**
+     * Set the speaker volume to a new value ( between 0 and 100 )
+     * @param volume  The new value
+     */
+    void setPlaybackVolume( double volume );
+
   private:
     /**
      * Drop the pending frames and close the capture device
-- 
GitLab