Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Flac to mp3

Converts all *.flac files in the current dir to mp3.

#!/bin/bash

for file in *.flac
do
	echo Converting $file
	flac123 -q --wav=- "$file" | lame - "$file".mp3
done

Last.fm favorite artists for social network

// Returns a list of your top 50 favorite artists from last.fm, suitable for pasting into a Facebook, Myspace, or similar profile

#!/usr/bin/env ruby
require 'net/http'
require 'csv'
user = 'nertzy'
csv = Net::HTTP.get 'ws.audioscrobbler.com', "/1.0/user/#{user}/topartists.txt"
puts CSV::Reader.parse(csv).collect{ |row| row[2] }.join(", ")

Java - Example Very Simple Player (JMF)

// Main Class

package org.jmf.example;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class ExampleJMF
{
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true); 
		JDialog.setDefaultLookAndFeelDecorated(true);
		
		try
		{
			UIManager.setLookAndFeel(new MetalLookAndFeel());
		}
		catch(UnsupportedLookAndFeelException e)
		{
			e.printStackTrace();
		}
		
		new exampleFrame();
	}
}


// Frame Class

package org.jmf.example;

import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class exampleFrame extends JFrame
{
	private static final long serialVersionUID = 1L;
	
	public exampleFrame()
	{
		super("JMF - Example...");
		
		setSize(400, 300);
		setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height - getHeight())/2);
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent evt)
			{
				System.exit(0);
			}
		});
		
		setContentPane(new examplePanel());
		setVisible(true);
	}
}


// Panel Class

package org.jmf.example;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.swing.JPanel;

public class examplePanel extends JPanel implements ActionListener, ControllerListener
{
	private static final long serialVersionUID = 1L;
	
	private Component visualComponent;
	private Player player;
	
	public examplePanel()
	{
		try
		{
			player = Manager.createPlayer(new URL("file:///tmp/a.mpg"));
			player.addControllerListener(this);
			
			player.start();
		}
		catch(NoPlayerException e)
		{
			e.printStackTrace();
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
	}

	public void actionPerformed(ActionEvent e)
	{

	}

	public void controllerUpdate(ControllerEvent c)
	{
		if(player == null)
			return;
		
		if(c instanceof RealizeCompleteEvent)
		{
			if((visualComponent = player.getVisualComponent()) != null)
				add(visualComponent);
		}
	}
}

PHP last.fm chart creator

This code displays the Top 50 artists XML feed from last.fm in a nice presentable way on your website. Can easily be modified to work with any other feed from last.fm, find the full guide at http://www.strydominc.za.net/index.php?p=projectdetail&d=phplastfmchart

<?php
/**********************************************************************************************
www.strydominc.za.net
Created by Jurgen Strydom, 19-08-2006, jurgen.strydom@gmail.com
Read the readme.txt
Version 1.01, 05-09-2006
**********************************************************************************************/
?>
<link href="lastfmbar.css" rel="stylesheet" type="text/css">
<div>
<?php
//User settings -> needs your attention
$user = "Alkine"; //Your username
$width = 700; //width of the list

//Code you should not worry about
$file = "http://ws.audioscrobbler.com/1.0/user/$user/topartists.xml";
$xml = simplexml_load_file("$file");
$big = $xml->artist[0]->playcount;
$total = count($xml->artist);
$factor =  $width /$big;
?>
<table width="<?php echo $width ?>" border="0" cellpadding="0" cellspacing="0">  
 <?php
 for ($k=0 ; $k<=$total - 1; $k++) {
 	$barlen = round(($xml->artist[$k]->playcount * $factor), 0);
 ?>
  <tr>
    <td width="<?php echo $width ?>" height="10" valign="center"><table width="100%" border="0" cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr>
        <td width="<?php echo $barlen ?>" height="10" valign="center" class="lastfmbar"><?php 
		if ($barlen >= (($width + 100) /2)) {
			echo "<div align=\"right\">", $xml->artist[$k]->playcount, " - <b>", $xml->artist[$k]->name ,"</b></div>";
		}
		if (($barlen < (($width + 100) /2)) && ($barlen >= ($width / 3))) {
			echo "<div align=\"right\">", $xml->artist[$k]->playcount ," -</div>";
		}
		if ($barlen < ($width / 3)) {
			echo "&nbsp;";
		}		
		?></td>
        <td width="<?php echo $width - $barlen ?>" valign="center"><?php 
		if ($barlen >= (($width + 100) /2)) {
			echo "&nbsp;";
		}
		if (($barlen < (($width + 100) /2)) && ($barlen >= ($width / 3))) {
			echo "<div align=\"left\"><b>&nbsp;", $xml->artist[$k]->name ,"</b></div>";
		}
		if ($barlen < ($width / 3)) {
			echo "<div align=\"left\">", $xml->artist[$k]->playcount, " - <b>", $xml->artist[$k]->name ,"</b></div>";
		}		
		?></td>
      </tr>
    </table></td>
  </tr>
  <?php
  }
  ?>
</table>
</div>
<?php
/**********************************************************************************************
Changelog:

Version 1.01
Fixed a bug that caused the bar sizes to display incorrectly.

Version 1
First release.
**********************************************************************************************/
?>

Generate and play midi on mobile phone

From a previous snippet, you can play any sound file
(including midi) with pys60.
http://bigbold.com/snippets/posts/show/400

Now, what if you can generate a midi file as well?
I found a pure python midi file library.
http://www.mxm.dk/products/public/pythonmidi

I modify it a bit, just to make a single file for easy download.
http://larndham.net/service/pys60/smidi.py
With it, you can play a single note with the following code.
>>> import smidi
>>> m = smidi.MidiOutFile('C:\\out.mid')
>>> m.header()
>>> m.start_of_track()
>>> m.update_time(0)
>>> m.note_on(note=0x40)  # single note
>>> m.update_time(192)
>>> m.note_off(note=0x40) # stop it after 192
>>> m.update_time(0)
>>> m.end_of_track()
>>> m.eof()

>>> from audio import Sound
>>> s = Sound.open('C:\\out.mid')
>>> s.play()
>>> s.close()

These 10 lines can be made into a simple function.
Then, you can just type a line and play any note you like
on you mobile phone.
>>> from smidi import play
>>> play([(64,192), (32, 192)])

Ah... so pythonic. ^_^

Playing sound

py_s60 1.1.3 provides audio module which allow you to
play sound. This should let people write some fun games.

Here's a quick example.
from audio import *
f = 'C:\\Nokia\\Sounds\\Digital\\28050.amr'
s = Sound.open(f)
s.play()

I have tried calling Sound.open(f).play() directly, but
it doesn't seem to work. I may be waiting for file loading
or wait for some callback. I don't know.

You can play it many times or repeat forever
s.play(3) # 3 times
s.play(KMdaRepeatForever) # or just use -2
s.stop() # Ok, enough of it
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS