Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
added mute command; make it work with .net 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-perez24 committed Apr 12, 2016
1 parent 5e7ad3f commit c508fa1
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 43 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Wox.Plugin.Volume
Control the system volume with [Wox](http://www.getwox.com/).

## Usage
Keyword: `vol`

Type `+` to increase the volume, `-` to decrease the volume, and `m` to mute. If you type something like `++-`, it will increase twice, and decrease once!
The increase/decrease step, and the keys to control the volume can be configured: just edit the `config.json` file and restart Wox.

You can also change the style: default style (`percent`) shows a percentage; `bar` will show a ASCII volume bar.

## Gift
Feel free to offer me some nice icons :)

## Disclaimer
I don't have sound keys at work..
Binary file removed Wox.Plugin.Volume.wox
Binary file not shown.
6 changes: 6 additions & 0 deletions Wox.Plugin.Volume/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
{
class Config
{
public const string STYLE_BAR = "bar";
public const string STYLE_PERCENT = "percent";

public int step { get; set; }
public string up { get; set; }
public string down { get; set; }
public string mute { get; set; }
public string style { get; set; }
public bool applyOnDelete { get; set; }
}
}
Binary file added Wox.Plugin.Volume/CoreAudio.dll
Binary file not shown.
Binary file removed Wox.Plugin.Volume/CoreAudioApi.dll
Binary file not shown.
Binary file modified Wox.Plugin.Volume/Images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Wox.Plugin.Volume/Images/icons.xcf
Binary file not shown.
Binary file added Wox.Plugin.Volume/Images/mute.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 81 additions & 34 deletions Wox.Plugin.Volume/Main.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using CoreAudioApi;
using System.IO;

namespace Wox.Plugin.Volume
{
public class Main : IPlugin
{
private PluginInitContext context;
private MMDevice PlaybackDevice;
private Manager volumeManager;
private Config config;
private bool initialized = false;
private string initializaionError;
private string lastQuery = "";

public void Init(PluginInitContext context)
{
this.context = context;


// 1. read the config.json
try
{
string json = (new StreamReader(context.CurrentPluginMetadata.PluginDirectory + "\\config.json")).ReadToEnd();
config = (new JavaScriptSerializer()).Deserialize<Config>(json);
using (StreamReader sr = new StreamReader(context.CurrentPluginMetadata.PluginDirectory + "\\config.json"))
{
config = (new JavaScriptSerializer()).Deserialize<Config>(sr.ReadToEnd());
}
}
catch (System.Exception)
catch (Exception e)
{
initializaionError = e.Message;
return;
}

// 2. initialize the volume handling
try
{
PlaybackDevice = MMDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
volumeManager = new Manager();
}
catch (CoreAudioException)
catch (Exception e)
{
initializaionError = e.Message;
return;
}

// 3. all good
initialized = true;
}

Expand All @@ -50,59 +59,97 @@ private Result GetResult(Query query)
Result result = new Result()
{
Title = "Current volume",
SubTitle = "no info",
SubTitle = initializaionError,
IcoPath = "Images\\icon.png"
};

// initialization failed, we can't process the input
if (!initialized)
{
return result;
}

string queryString = query.ToString();
string lastChar = queryString.Substring(queryString.Length - 1);
string queryString = query.RawQuery;

int volume = GetVolume();
if (lastChar.Equals(config.up))
{
volume += config.step;
}
else if (lastChar.Equals(config.down))
// do nothing if the config says so and the user deleted the input
if (config.applyOnDelete || !config.applyOnDelete && lastQuery.Length < queryString.Length)
{
volume -= config.step;
// process the last char
ApplyKey(
queryString.Substring(queryString.Length - 1)
);
}

// write the volume description in the result
result.SubTitle = GetVolumeDescription();

SetVolume(volume);
volume = GetVolume();
// store the query as last query
lastQuery = queryString;

string volumeDesc = "[";
for (int i = 0; i < 100; i += config.step)
// change the icon if muted
if (volumeManager.IsMute())
{
volumeDesc = string.Concat(volumeDesc, volume > i ? "=" : "-");
result.IcoPath = "Images\\mute.png";
}
volumeDesc = string.Concat(volumeDesc, "]");

result.SubTitle = volumeDesc;
return result;
}

public int GetVolume()
private string GetVolumeDescription()
{
return (int) (PlaybackDevice.AudioEndpointVolumeEx.GetMasterVolumeLevelScalar() * 100);
if (volumeManager.IsMute())
{
return "[X]";
}

int volume = volumeManager.GetVolume();
string volumeDesc;
switch (config.style)
{
case Config.STYLE_PERCENT:
volumeDesc = volume + "%";
break;

case Config.STYLE_BAR:
volumeDesc = "[";
for (int i = 0; i < 100; i += config.step)
{
volumeDesc = string.Concat(volumeDesc, volume > i ? "=" : "-");
}
volumeDesc = string.Concat(volumeDesc, "]");
break;

default:
volumeDesc = "";
break;
}

return volumeDesc;
}

public void SetVolume(int volume)
private void ApplyKey(string lastChar)
{
if (volume < 0)
if (lastChar.Equals(config.mute))
{
volumeManager.ToggleMute();
}

if (volumeManager.IsMute())
{
return;
}

int volume = volumeManager.GetVolume();
if (lastChar.Equals(config.up))
{
volume = 0;
volume += config.step;
}
else if (volume > 100)
else if (lastChar.Equals(config.down))
{
volume = 100;
volume -= config.step;
}

PlaybackDevice.AudioEndpointVolumeEx.SetMasterVolumeLevelScalar(volume / 100.0f);
volumeManager.SetVolume(volume);
}

}
Expand Down
43 changes: 43 additions & 0 deletions Wox.Plugin.Volume/Manager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CoreAudio;

namespace Wox.Plugin.Volume
{
class Manager
{
private MMDevice playbackDevice;

public Manager()
{
playbackDevice = (new MMDeviceEnumerator()).GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
}

public int GetVolume()
{
return (int)(playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
}

public void SetVolume(int volume)
{
if (volume < 0)
{
volume = 0;
}
else if (volume > 100)
{
volume = 100;
}

playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar = volume / 100.0f;
}

public void ToggleMute()
{
playbackDevice.AudioEndpointVolume.Mute = !IsMute();
}

public bool IsMute()
{
return playbackDevice.AudioEndpointVolume.Mute;
}
}
}
14 changes: 7 additions & 7 deletions Wox.Plugin.Volume/Wox.Plugin.Volume.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Wox.Plugin.Volume</RootNamespace>
<AssemblyName>Wox.Plugin.Volume</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
Expand All @@ -34,26 +34,26 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="CoreAudioApi">
<HintPath>.\CoreAudioApi.dll</HintPath>
<Reference Include="CoreAudio, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\CoreAudio.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Wox.Plugin, Version=1.1.1.295, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Wox.Plugin.1.1.1.295\lib\net35\Wox.Plugin.dll</HintPath>
<Reference Include="Wox.Plugin, Version=1.1.0.268, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Wox.Plugin.1.1.0\lib\net35\Wox.Plugin.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="Main.cs" />
<Compile Include="Manager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion Wox.Plugin.Volume/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"step": 5,
"up": "+",
"down": "-"
"down": "-",
"mute": "m",
"style": "percent",
"applyOnDelete": false
}
2 changes: 1 addition & 1 deletion Wox.Plugin.Volume/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Wox.Plugin" version="1.1.1.295" targetFramework="net452" />
<package id="Wox.Plugin" version="1.1.0" targetFramework="net35" />
</packages>

0 comments on commit c508fa1

Please sign in to comment.