Saturday 30 April 2011

Controlling Rhythmbox/banshee from shell

In Linux all the music player like Banshee / Rhythmbox music player has a dbus interface which provides us a means to control them from commandline like pause, play, next, previous, increase/decrease volume.

I wrote few alias to do so ...

Rhythmbox

alias pauseplay='dbus-send --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.playPause boolean:true'

alias next='dbus-send --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.next'

alias prev='dbus-send --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.previous'

alias incvolume='dbus-send --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.setVolumeRelative double:.1'

alias decvolume='dbus-send --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.setVolumeRelative double:-.1'

Banshee
alias pauseplay='dbus-send --type=method_call --dest=org.bansheeproject.Banshee /org/bansheeproject/Banshee/PlayerEngine org.bansheeproject.Banshee.PlayerEngine.TogglePlaying'

alias next='dbus-send --type=method_call --dest=org.bansheeproject.Banshee /org/bansheeproject/Banshee/PlaybackController org.bansheeproject.Banshee.PlaybackController.Next boolean:false'

alias prev='dbus-send --type=method_call --dest=org.bansheeproject.Banshee /org/bansheeproject/Banshee/PlaybackController org.bansheeproject.Banshee.PlaybackController.Previous boolean:false'



Place these alias in your .bashrc or .bash_aliases file. Now you can control rhythmbox from shell like
pauseplay - play or pause rhythmbox Music player
next - next track
prev - previous track
incvolume - increase volume
decvolume - decrease volume

Change the alias for your convenience ...

Thursday 28 April 2011

Odd or Even number

Given a positive number n print whether the number is Even or Odd without using any conditional and comparison operators.




int main()
{
int number;
char* names[2] = {"Even","Odd"};
printf("Enter a positive number:");
scanf("%d", &number);
printf("\n%d is %s number\n", number, names[number % 2]);
}



Tuesday 26 April 2011

Puzzle 1 : Survivor Problem

Problem Statement : There are 100 people forming a circle and they are numbered from 1 to 100. They decide to kill each other using a sword, in following manner. At any point, person numbered n having a sword kills person numbered n+1 and passes the sword to n+2. This process doesnot stop until at the end only 1 person remains alive. Initially, person numbered 1 has the sword in hand. Calculate the survivor number. What about any general N ?

Solution


For various N, survivor number obtained is given below,





1 -> 12 -> 14 -> 18 -> 1

3 -> 35 -> 3


6 -> 5


7 -> 7



It is apparent from above pattern that when N is power of 2, Survivor is 1. Otherwise we have to add 2 to every number away from power of 2.



Method 1(Using Recursion):

  1. Let Survivor number be 1

  2. Until N is power of 2


    1. Subtract N by 1

    2. Add 2 to Survivor number





int checkPowerOfTwo(int num)
{
return (num && !(num & (num - 1)));
}

int findSurvivor(int totPerson)
{
int result = 1;
while(!checkPowerOfTwo(totPerson)
&& totPerson > 0)
{
findSurvivor(--totPerson);
result += 2;
}
return result;
}

int main (int argc,
char *argv[])
{

int noOfPerson;

// Check number of arguments passed
if (argc != 2)
{
printf( "usage: %s N\n", argv[0] );
printf( "\t where N is number of persons\n", argv[0] );
return 1;
}

noOfPerson = atoi(argv[1]);

if (noOfPerson == 0)
{
printf("Zero is not a valid input\n");
return 2;
}
printf("Survivor is %d\n", findSurvivor(noOfPerson));
return 0;
}




Method 2:

  1. If N is power of 2, return 1

  2. Find next lower power of 2 wrt to N

  3. Find difference between N and result from step 2. Double and add 1





    1. int checkPowerOfTwo(int number)
      {
      return (number && !(number & (number - 1)));
      }

      int findIndexOfMsb(int number)
      {
      int result = 0;
      while (number >>= 1)
      {
      result++;
      }
      return result;
      }
      int findSurvivor(int totPerson)
      {
      if (checkPowerOfTwo(totPerson))
      {
      return 1;
      }
      return (1 + (totPerson - (1 << findIndexOfMsb(totPerson))) * 2);
      }

      int main (int argc,
      char *argv[])
      {

      int noOfPerson;

      // Check number of arguments passed
      if (argc != 2)
      {
      printf( "usage: %s N\n", argv[0] );
      printf( "\t where N is number of persons\n", argv[0] );
      return 1;
      }

      noOfPerson = atoi(argv[1]);

      if (noOfPerson == 0)
      {
      printf("Zero is not a valid input\n");
      return 2;
      }
      printf("Survivor is %d\n", findSurvivor(noOfPerson));
      return 0;
      }



Thursday 21 April 2011

Convert: The Image Conversion Tool

Many times command line tools are so cool and able than a GUI. I got to know about such a command line image conversion tool. It helped me complete my work so fast and efficient than many GUIs.

This tool does not come by default in ubuntu 10.10. You need to install imagemagick.

sudo apt-get install imagemagick

Giving you glimpse of what this amazing tool can do ... This is just a tip of the iceberg. You can do much much more using this ...

  1. Convert an Image from one Format to another (You can give any image format you know)

      $ convert a.jpg b.png

      $ convert a.bmp c.jpg

  2. To convert all files in folder

      $ convert *.jpg -set filename:name '%t' '%[filename:name].png'

  3. To convert all files in a folder and rename it as per your prefix

      $ convert *.jpg mynameprefix.png'

  4. To create a gif animation from the image files you have

      $ convert *.jpg mygif.gif

  5. To add a pause between transition in a gif image

      $ convert *.jpg -delay 100 mygif.gif

  6. To resize an image

      $ convert myimg.jpg -resize 50% myimgsmall.jpg

      $ convert myimg.jpg -resize 120x120 mythumbnail.jpg

        - To resize the image to pixel of 120 x 120 (width x height) image

  7. To add images to a pdf

      $ convert *.jpg photos.pdf

  8. Sometimes the image size will be too big but you can reduce it by reducing the quality(You may not be able to perceive it in computer screen)

      $ convert img.jpg -quality 75 new_img.jpg

      $ convert *.jpg -quality 75 mynameprefix.jpg

        - To convert all the jpg files and rename it to required prefix followed by sequential number

      $ convert *.jpg -quality 75 -set filename:name '%t' '%[filename:name].jpg'
      - To lower the quality without changing their name.

  9. To Add string to your file at a given location (0,0 denotes top left)

      $ convert myimg.jpg -draw 'text 100,200 “MY STRING”' mynewimg.jpg

      $ convert myimg.jpg -pointsize 20 -draw 'scale 1.5,1.5 text 100,200 “MY STRING”' mynewimg.jpg

          - This makes font size 20

      $ convert *.JPG -set filename:name '%t' -pointsize 36 -draw 'text 500,200 "COOL STUFF"' '%[filename:name].JPG'

  10. To make an image black and white

      $ convert myimg.jpg -colorspace gray myblack.jpg

      $ convert myimg.jpg -charcoal 5 myfun.jpg

        - It converts an image to pencil sketch the number indicates the thickness of line


For Further hacks refer to

http://www.imagemagick.org/Usage/