Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Friday, May 25, 2007

Audio file conversion

Here's a collection of small scripts for converting audio file formats using lame, mpg123, and mplayer.

wav2mp3.pl

#!/usr/bin/perl
use strict;
use warnings;

my $in = "/home/doug/music/somedir/";
my $out = $in;

opendir DH_IN, $in || die "Can't open $in: $!";
foreach my $name (sort readdir(DH_IN)) {
next unless $name =~ /\.wav$/;
print "NAME: $name\n";
my($base_name) = ($name =~ /^(.*)\.wav$/);
my $in_file = "${in}$base_name.wav";
my $out_file = "${out}$base_name.mp3";
`lame -b 192 $in_file $out_file`;
}
close DH_IN;


wav2mp3.sh

#!/bin/sh
bitrate="192"
for wavfile in *.wav
do
echo "input: $wavfile"
mp3file=`echo $wavfile | sed s/\\.wav/.mp3/`
echo "output: $mp3file"
lame -b $bitrate $wavfile $mp3file
done


mp32wav.sh

#!/bin/sh
for mp3file in *.mp3
do
echo "input: $mp3file"
wavfile=`echo $mp3file | sed s/\\.mp3/.wav/`
echo "output: $wavfile"
mpg123 -w $wavfile $mp3file
done


wma2mp3.sh

#!/bin/sh

if [ -z "$1" ]
then
echo "Usage: wma2mp3 "
exit 1
fi

name=`echo $1 | tr ' ' '_' | tr '[A-Z]' '[a-z]'`

name="`basename "$name" .wma`.mp3"

cp "$1" $name

mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader $name && lame -m s -h --vbr-new audiodump.wav -o $name


m4a2mp3.sh

#!/bin/sh

MPLAYER=mplayer
LAME=lame

for m4a in *.m4a
do

mp3=`echo "$m4a" | sed -e 's/m4a$/mp3/'`
wav="$m4a.wav"

if [ ! -e "$mp3" ]; then
[ -e "$wav" ] && rm "$wav"
mkfifo "$wav" || exit 255
mplayer "$m4a" -ao pcm:file="$wav" &>/dev/null &
lame "$wav" "$mp3"
[ -e "$wav" ] && rm "$wav"
fi
done

Finding installed Perl modules

I don't recall where I came across this little script, but it's quite handy for finding installed Perl modules along with their versions.


#!/usr/bin/perl
use strict;

use ExtUtils::Installed;
my $instmod = ExtUtils::Installed->new();
foreach my $module ($instmod->modules()) {
my $version = $instmod->version($module) || "???";
print "$module -- $version\n";
}

Wednesday, May 16, 2007

iBATIS - part 1

Very basic and simple iBATIS tutorial for a simple SELECT:

Download iBATIS from http://ibatis.apache.org/

Unpack the tar ball and install the jar file (it's now a single file - something like ibatis-2.3.0.677.jar) somewhere on your classpath...or if using with Tomcat or another servlet container, add it to "/WEB-INF/lib" in your for the webapp.

Database (MySQL)

mysql> describe book;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(50) | NO | | | |
| authors | varchar(50) | YES | | NULL | |
| editor | varchar(50) | YES | | NULL | |
| chapters | text | YES | | NULL | |
| page_count | int(11) | YES | | NULL | |
| status | char(1) | NO | | P | |
| price | decimal(5,2) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+

mysql> SELECT * FROM book;
+----+----------------------+---------+--------+----------+------------+--------+-------+
| id | title | authors | editor | chapters | page_count | status | price |
+----+----------------------+---------+--------+----------+------------+--------+-------+
| 1 | Learning Perl 3rd Ed | NULL | NULL | NULL | NULL | P | 29.99 |
| 2 | Learning Java 4th Ed | NULL | NULL | NULL | NULL | P | 29.99 |
+----+----------------------+---------+--------+----------+------------+--------+-------+


SqlMapConfig.xml

PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>

<!-- These settings control SqlMap configuration details -->
<settings
useStatementNamespaces="true"
/>

<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/bookdb"/>
<property name="JDBC.Username" value="yourusername"/>
<property name="JDBC.Password" value="yourpassword"/>
</dataSource>
</transactionManager>

<!-- Identify all SQL Map XML files to be loaded by this SQL map -->
<sqlMap resource="BookMap.xml" />

</sqlMapConfig>


BookMap.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Book">

<resultMap id="get-Result" class="Book">
<result property="id" column="id"/>
<result property="title" column="title"/>
</resultMap>

<select id="getById" parameterClass="string" resultMap="get-Result">
SELECT * FROM book WHERE (id = #value#)
</select>

<select id="getAll" parameterClass="string" resultMap="get-Result">
SELECT * FROM book WHERE (status = 'P') ORDER BY title
</select>

</sqlMap>


Book.java

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.Date;

public class Book {

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getTitle()
{
return title;
}

public void setTitle(String title)
{
this.title = title;
}

public char getStatus()
{
return status;
}

public void setStatus(char status)
{
this.status = status;
}

private Integer id;
private String title;
private char status;
}


BookTest.java

import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.Resources;

import java.io.Reader;
import java.util.List;
import java.util.Iterator;

public class BookTest {
public static void main(String arg[]) throws Exception {
String resource = "SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
List bookList = sqlMap.queryForList("Book.getAll",null);
System.out.println("Selected " + bookList.size() + " records.");

if(bookList.size() == 0){
System.out.println("OOOPS - bookList size is 0\n");
}
Iterator books = bookList.iterator();
StringBuffer bookBuffer = new StringBuffer();
while(books.hasNext()){
Book bookX = (Book)books.next();
bookBuffer.append("ID: " + bookX.getId() + " TITLE: " + bookX.getTitle() + "\n");
}
String bookOptions = bookBuffer.toString();
System.out.println(bookOptions);
}
}



$ java BookTest
Selected 2 records.
ID: 2 TITLE: Learning Java 4th Ed
ID: 1 TITLE: Learning Perl 3rd Ed

Sunday, May 13, 2007

Tomcat - How to run stand alone on port 80

With Apache, all ports below 1024 are privileged ports, so normally to run Tomcat as a stand alone server, you'd have to run as root, which is a Very Bad Idea. Using the jsvc interface, it is possible to bind Tomcat to a privileged port, such as port 80.

First, change server.xml to use port 80:


$ cd $CATALINA_HOME/conf
$ sudo vi server.xml


and make this change:


<!--<Connector port="8080" protocol="HTTP/1.1" -->
<Connector port="80" protocol="HTTP/1.1"


With Tomcat 5 & 6, there will be a jsvc.tar.gz file in the bin directory of your $CATALINA_HOME directory.


$ cd $CATALINA_HOME/bin
$ sudo tar zxvf jsvc.tar.gz
$ cd jsvc-src
$ sudo autoconf
$ sudo ./configure
$ sudo make
$ sudo cp jsvc ..


I had to add a few packages on Ubuntu to compile jsvc...

$ sudo apt-get install libc6-dev g++ gcc
$ sudo apt-get install make
$ sudo apt-get install autoconf


You can start Tomcat as "tomcat" user (use which ever existing non-privileged user you want, or create a new user):


$ cd $CATALINA_HOME
$ sudo ./bin/jsvc -cp ./bin/bootstrap.jar \
-user tomcat \
-outfile ./logs/catalina.out \
-errfile ./logs/catalina.err \
org.apache.catalina.startup.Bootstrap


There is a sample starup script in the $CATALINA_HOME/bin/jsvc-src/native directory, which can be modified to work on your system:


$ cd /etc/init.d
$ sudo cp tomcat tomcat.bak (if needed, and make sure it will start on the run levels you need for startup on server boot)
$ sudo cp $CATALINA_HOME/bin/jsvc-src/native/Tomcat5.sh ./tomcat
$ sudo vi tomcat (edit for your system)


Now you can start Tomcat:


$ sudo /etc/init.d/tomcat start


More info can be found at Apache Jakarta Project - commons daemon (Java Service)

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels