Saturday, March 08, 2014

[Ubuntu] Streaming your PC / Laptop audio stream (alsa) to another LAN connected device

I constantly like to change where in the room I sit for some computing. I get bored too easily in one place so I keep switching places. But, sadly, my sound system isn't wireless. So, how to fix this issue? I wanna move around but still want all my songs keep sounding through my central surround sound? WiFi. Yep.
With the help of the amazing power of Linux, it's too much cute sound architecture ALSA and a little SSH, I stream my whole soundcard feed to another "Old, and slow" laptop that stays with my sound system. I tried Pulseaudio, but it didn't quite get me where I wanted to be. I just use it for selected which feed goes to the other laptop and which stays native.
Stictly, Ubuntu instructions
Requirements
For this work, you need
1. An extra Linux machine (Raspberry Pi/another Laptop/Netbook anything that can run ssh-servr)
2. WiFi network setup (Router, tethered from Laptop etc) 3. A little bug fixing here and there if this tutorial becomes too old

First of all, let's install the following packages
pulseaudio paprefs pavucontrol sox [on the machine that sends the sound data]
openssh-server [on the machine that receives sound data]
I'm assuming both laptops have working sound system setup.

Then, on the streamer (the machine from which we'll send the sound) run this command
modprobe snd-aloop index=0 pcm_substreams=1
This is a module you must load in order to get a loop back device in your soundcards list. What this device does is put all your audio data so that any device can pick that from there. In this case my Laptop connected to the speakers.
If you happen to fail to load this module, check for some instruction on the internet. I found some tutorials that may help you to resolve issues. These are two good tutorials
http://confoundedtech.blogspot.com/2012/08/building-installing-alsa-loopback.html
http://www.sm5bsz.com/linuxdsp/install/snd-aloop.htm
To be able to load this automatically on each boot just add this to
/etc/modules

Now, when your modules is up, you should be able to see a loop back device in your "Sound Settings" that is called "Analog Output, Loopback device"
Now open up ~/.asoundrc and add this to that, if the file doesn't exist create it.
pcm.!default {
  type dmix
  slave.pcm "hw:Loopback,0,0"
}
pcm.loop {
  type plug
  slave.pcm "hw:Loopback,1,0"
}
This basically puts all your sound data in the loopback device. If you want to select manually instead use this.
pcm.loop {
  type plug
  slave.pcm "hw:Loopback,1,0"
}
Save the file. Now, the configuration part is over. You just need to reboot your system. If after rebooting your system does not come up with the loopback device, just load the module again. Now to access the sound on the other device, log into it (or ssh into it from the source machine) and run this command.
ssh -C streamer_username@lan.ip.of.streamer sox -q -t alsa loop -t wav -b 24 -r 48k - | play -q -
This basically will, log in from the laptop that is connected to the sound system to my workspace machine and create a stream file that will be fed back to it via ssh. Then that stream file will be played out on that machine. Neat right?
Afterwards when you are playing something, just run the command
pavucontrol
And from the list of running sound feeds, redirect anyone (or all, your wish) to the "Loopback Audio" device via the menu on the right.

I researched a lot for this setup. Initially my searches kept telling me that I should use "pulseaudio" to expose sound cards to the network, from which the player machine can pick up it's feed and play it back through the system. That setup worked but had a lot of interceptions in the output. The problem might have been of pulseaudio alone. I had almost lost hope but I accidentally stumbled upon these two tutorials that saved me. Thanks a lot to them
http://ywwg.com/wordpress/?cat=13
http://plasmasturm.org/log/soundserverhack/
Both of them are amazing write ups.
Gotchas
Even though this system works flawlessly in quality of output, but it has a huge disadvantage of using up many resources and having a huge sync flaw with videos. But I resolved it by using VLC to adjust it, via the J and K keys.

Thursday, January 23, 2014

[OpenWRT] Configure ZTE MF193E (Teletalk) to autoconnect on TP-Link MR3420 V2.0

WARNING: Anything you might do to your router / modem / computer, I can not be held responsible. This is just for referencial and educational purposes. This involves modifying certain core features of both the devices and they might not behave as you please afterwards.

As you might know, the new in thing is to buy a router and then, flash an after market firmware. And the very first one in line is OpenWRT.

Currently TL-MR3420 (here on referred to as the Router) has only snapshot / trunk build available of OpenWRT. You can get the V2 firmware here [ http://downloads.openwrt.org/snapshots/trunk/ar71xx/openwrt-ar71xx-generic-tl-mr3420-v2-squashfs-factory.bin ]. This is pre built one.

Flash using the normal process of "Firmware Upgrade". You'll lose WiFi communication. So, connect with a LAN cable that goes from your Laptop / PC to the routers LAN (not the blue WAN port) ports. Type in 192.168.1.1 and you got your OpenWRT running.
Set it up, secure the WiFi and Admin panel first. Look at the myriads of options you have now.

Now, for the modem. Well, this would not be an issue at all if the modem did not have a CD-Mode on it. The main problem in connecting is the CD-Mode. And this particular version of OpenWRT works pretty well at handling things as it has all the various packages needed, pre installed (hence leaves very little on your router, might consider expanding storage with an SD card?)

We have different choices to circumvent the issue
* USB Modeswitch
* Sdparm

Both have worked for me. It's your choice which one you want to apply.
First things first, detect and check if your device has come up on the system.
Log into your Wifi. Then open up a terminal (ubuntu will do) and type
ssh root@192.168.1.1
If you get issues with invalid ssh key just execute the command the message shows.
Type in your password and you'll be in the router (Trinity hack huh?)
Now type
dmesg | grep ZTE
It should show your Modem with a CD-ROM beside it.
Now, you need to know your vendor and product ID. Fortunately if you are using mine it is Vendor: 19d2 and Product: 1232. To find it yourself, type
cat /sys/kernel/debug/usb/devices
and look for your modem's name. Around it, somewhere you'll see Vendor and ProdID. Make a note of them.
Before we move on to switching the mode First one, USB Modeswitch
Basically you have to move the modem from 19d2:1232 to 19d2:2003. For that you can use
usb_modeswitch -c /etc/usb_modeswitch.d/19d2\:1013 -v 19d2 -p 1232
Or you can disable the CD-ROM mode using either 60-usb-rules or some init.d / rc.d hack or simply using the AT commands
AT+ZCDRUN=8 disable
AT+ZCDRUN=9 enable
How to use the AT commands? Do this
echo "<at command here>" >/dev/ttyUSB1
And then, go to Interfaces > 3G > Edit and choose /dev/ttyUSB3
Use APN as "wap" Protocol as "UMTS/GPRS/EV0" and Sevice type as "UMTS/GPRS" and Save it.

And to mount SD card and increase your storage first check if you have proper fs modules. My card couldn't take beyond ext2 so I used ext2 for fs. That part was confusing a bit at first since the version does not have warnings at all :P
Hope it comes handy :)
If you'd like to post about some of your own approaches I'd be happy to add them here :)
Just mail me if you have any or comment below.

Credits due:
1. http://www.semanticlab.net/index.php/UMTS_with_OpenWRT
2. http://askubuntu.com/questions/31477/use-a-newer-zte-modem-with-usb-modeswitch
3. http://forum.xda-developers.com/showthread.php?t=1735553

Monday, December 09, 2013

[UVa] 12718 - Dromicpalin Substrings

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

bool isOdd(int val) {
    if (val & 1) return true; // bitwise checking
    else return false;
}
int counts[1000]; // only 'a' - 'z' is significant
int main( ) {

    //freopen("j.input.txt","r",stdin);
    //freopen("j.analysis.txt","w",stdout);

    int test, kase=1;
    string inputstr;

    cin >> test;

    while (test--) {

        cin >> inputstr;

        int palindromes=0;
        for (int i = 0 ; i<inputstr.size() ; i++) {
            for (int ii = 'a' ; ii<='z' ; ii++) counts[ii] = 0;
            int odds = 0;
            for (int j=i ; j<inputstr.size() ; j++) {
                counts[inputstr[j]]++;

                if ( isOdd(counts[inputstr[j]]) ) odds++;
                else odds--;
                int rangeLength = j-i+1;
                if ( isOdd(rangeLength) && odds==1 ) palindromes++;
                if ( !isOdd(rangeLength) && odds==0 ) palindromes++;
            }
        }
        cout << "Case " << kase++ << ": " << palindromes << endl;
    }

    return 0;
}


[UVa] 12712 - Pattern Locker

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

#define MOD 10000000000007

typedef long long lint;

int main() {

    lint test, l, m, n, kase=1;

    cin >> test;

    while (test--) {

        cin >> l >> m >> n;
        lint init = 1;
        for (lint i = l*l ; i>(l*l-m) ; i--) {
            init = ((init * i)%MOD);
        }
        lint sum = init;
        for (lint i = (l*l-m) ; i>(l*l-n) ; i--) {
            init = ((init * i)%MOD);
            sum = ((sum + init)%MOD);
        }

        cout << "Case " << kase++ << ": " << sum << endl;


    }


    return 0;
}


Sunday, November 10, 2013

Using Google Chrome with a proxy server

Works: When there's no other Chrome instance is running already, that means the one you are going to open has to be the first
This method is useful for those who use a lot of proxy and the ones who use proxies for only specific sites.
To open Chrome with a proxy server support, use the following command (Note: Wasn't tried on Windows).
google-chrome --proxy-server="<host>:<port>"

Saturday, November 02, 2013

Convert images and apply various effects on them using the command line

One thing is for sure, something so beautiful as Linux needs exploring. You can't expect anyone to trully show it's virtues that might come in handy to you. Everybody has their own cut, you have to do your.
So this other minute I was looking for some cheatsheet wallpaper to put up on my Desktop. Got a lot of them. Chose one, but the images is Black text on white backgroud, a serious problem to a maniac like me. Figured it needs some invertion, but firing up GIMP for such trivial task? :/ After a bit of drooling on ideas, Imagemagick hit my mind. One of the best tools for batch processing of images using the terminal. To install, type in:
sudo apt-get install imagemagick
There are many many options and things that can be done using this gem, cropping, resizing, inverting, even combining multiple effects. Some example commands are listed below
# Change format
convert howtogeek.png howtogeek.jpg

# Enforcing compression level
convert howtogeek.png -quality 95 howtogeek.jpg

# Resizing
convert example.png -resize 200×100 example.png
# enforce specification
convert example.png -resize 200×100! example.png
# maintain ratio
convert example.png -resize 200 example.png
# or 
convert example.png -resize x100 example.png

# Rotation
convert howtogeek.jpg -rotate 90 howtogeek-rotated.jpg

# Effects
# charcoal
convert howtogeek.jpg -charcoal 2 howtogeek-charcoal.jpg
# implode
convert howtogeek.jpg -implode 1 howtogeek-imploded.jpg
# invert
convert howtogeek.jpg -negate imploded.jpg

# Combination
convert howtogeek.png -resize 400×400 -rotate 180 -charcoal 4 -quality 95 howtogeek.jpg
You should explore the endless possibilities yourself.
NOTE: The commands have been copied from www.howtogeek.com

Friday, November 01, 2013

Make a backup of your dotfiles

If you are a regular Linux user, chances are you change distros to experiment with them. I am one of such persons who like to experiment with various Linux distros for both educational and experimental reasons. This activity may sometimes lead to problems regarding your Desktop Environment (DE) and others. It also might be the case that you just want to clean up your /home but keep your configurations intact using your dotfiles (The directories and files in your /home whose name start with a '.'). Moving these files manually is cumbersome (at least for me). The following script does it for me. And moves it to a folder called "dotfiles" in your ~/.
CAUTION: It removes any folder called 'dotfiles" in you ~/ and creates a new one.
You MUST run this in your ~/
rm -rf dotfiles
mkdir dotfiles
while read line; do
        if [ "$line" = "" ]; then
                continue
        fi
        if [ -d "$PWD/$line" ]; then
                echo "$PWD/$line is a folder"
                cp $PWD/$line $PWD/dotfiles/$line # substitute cp with mv to move the files
        else
                echo "$PWD/$line is a file"
                cp $PWD/$line $PWD/dotfiles/$line # substitute cp with mv to move the files
        fi
done < <(find . -maxdepth 1 -name ".*" -printf '%P\n')

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...