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-10 of 11 total  RSS 

Play audio with a Ruby shell script

Play an ogg file from the command-line using the program ogg123 from the software package 'vorbis-tools'.

#!/usr/bin/ruby

#file: ogg-play.rb

`ogg123 #{ARGV[0]} > /dev/null 2>&1`



e.g.
wget http://upload.wikimedia.org/wikipedia/en/4/4d/Elo_blue_sky.ogg
./ogg-play.rb Elo_blue_sky.ogg

Using Espeak

Espeak is a Text-to-speech program which I have used on Ubuntu. A sample of the audio can be found from my twittergram [twittergram.com]. Note: I made a few spelling mistakes in the audio, listen and tell me if you can hear them.

espeak -f hello_jr -s 120

Convert a video file to an audio file (.mp4 to .mp3)

Convert an mp4 file to avi, then to mp3 (including mixing the stereo down to mono). This code was executed from the command-line on Ubuntu 7.04.

mencoder video.mp4 -ovc lavc -vf scale=123:100 -oac lavc -o video.avi
ffmpeg -i video.avi -ac 1 audio1.mp3 

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

How to make the system notify you of audio cd insertion

// Uses the DiskArbitration framework to notify ya of when new audio cds are inserted

NSDictionary *match = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"IOCDMedia", [NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:(NSString *)kDADiskDescriptionMediaKindKey, kDADiskDescriptionMediaWholeKey, nil]];
		
_session = DASessionCreate(kCFAllocatorDefault);
DASessionScheduleWithRunLoop(_session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
DARegisterDiskAppearedCallback(_session, (CFDictionaryRef)match, diskAppearedCallback, NULL);
DARegisterDiskDisappearedCallback(_session, (CFDictionaryRef)match, diskDisappearedCallback, NULL);

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);
		}
	}
}

Java - Mini Lettore Wav

// Piccolissimo Lettore Audio per Java

package Multimedia;

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

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


package Multimedia;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AudioGUI extends JFrame implements WindowListener
{
	public AudioGUI()
	{
		this.setTitle("AudioSystem");
		this.setSize(240, 100);
		this.setResizable(false);
		
		this.addWindowListener(this);
		
		Container gc = this.getContentPane();
		gc.add(new AudioGUIPan());
		
		this.setVisible(true);
	}

	public void windowOpened(WindowEvent e)
	{
		
	}

	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}

	public void windowClosed(WindowEvent e)
	{
		
	}

	public void windowIconified(WindowEvent e)
	{
		
	}

	public void windowDeiconified(WindowEvent e)
	{
		
	}

	public void windowActivated(WindowEvent e)
	{
		
	}

	public void windowDeactivated(WindowEvent e)
	{
		
	}
}

class AudioGUIPan extends JPanel implements ActionListener
{
	private JButton play;
	private JButton stop;
	private JButton loop;
	private JButton open;
	private JButton close;
	
	private JFileChooser jfc;
	
	private String file;
	
	private AudioDevice ad;
	
	public AudioGUIPan()
	{
		play = new JButton("Play");
		play.addActionListener(this);
		stop = new JButton("Stop");
		stop.addActionListener(this);
		open = new JButton("Open");
		open.addActionListener(this);
		close = new JButton("Close");
		close.addActionListener(this);
		
		this.add(stop);
		this.add(play);
		this.add(open);
		this.add(close);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == close)
		{
			System.exit(0);
		}
		else if(e.getSource() == open)
		{
			jfc = new JFileChooser();
			jfc.setFileFilter(new AudioFilter());
			
			if(jfc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION)
			{
				file = jfc.getSelectedFile().getAbsolutePath();
				ad = new AudioDevice(new File(file));
			}
		}
		else if(e.getSource() == play)
		{
			if(ad != null)
				ad.play();
		}
		else if(e.getSource() == stop)
		{
			if(ad != null)
				ad.stop();
		}
	}
}


package Multimedia;
import java.io.File;

import javax.swing.filechooser.FileFilter;

public class AudioFilter extends FileFilter
{
	public boolean accept(File f)
	{
		return f.getName().toLowerCase().endsWith(".wav") || f.isDirectory();
	}

	public String getDescription()
	{
		return "Audio File *.wav";
	}
}


package Multimedia;

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;

public class AudioDevice implements AudioClip
{
	private AudioClip ac;
	public AudioDevice(File f)
	{		
		try
		{
			ac = Applet.newAudioClip(f.toURL());
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
	}
	
	public void play()
	{
		ac.play();
	}

	public void stop()
	{
		ac.stop();
	}

	public void loop()
	{
		
	}
}

Python - Get id3 from MP3 File

// Cattura i tag id3 da un file MP3

def getID3(filename):
    fp = open(filename, 'r')
    fp.seek(-128, 2)

    fp.read(3) # TAG iniziale
    title   = fp.read(30)
    artist  = fp.read(30)
    album   = fp.read(30)
    anno    = fp.read(4)
    comment = fp.read(28)

    fp.close()

    return {'title':title, 'artist':artist, 'album':album, 'anno':anno}

sox audio editor settings for companding voice

I used the following sox audio editor settings to do dynamic compression on voice.

sox in.wav out.wav lowpass 4000 compand 0.1,0.3 -60,-60,-30,-15,-20,-12,-4,-8,-2,-7 -2

Recording sound

py_s60 1.1.3 provide audio module which allow you to record sound.
import e32, audio

s = audio.Sound.open('C:\\test.amr')
s.record()  # start recording
e32.ao_sleep(5)  # do if for 5 seconds
s.stop() # stop recording


# the file is now created, ready to be played
s.play()

Using this code, I can record sound longer than
the 1 minute limit by Nokia's 'Recorder' program.

Update:
It can record in 'wav', 'amr','au'.
I can't play the file recorded with 'wav', though.
It seems 6600 has the problem with uncompressed 16-bit wave.
(It can play other wave files fine.)
« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS