Trafegar VOZ

4 respostas
A

Pessoal preciso da ajuda de vocês.

Estou desenvolvendo um protótipo parecido com o Skype que utilizará redes P2P para se comunicar.

É um projeto de final de curso pois estou no 8°período de Ciência da Computação.

Mas o projeto é para inicio de Junho e não tenho nda proto ainda.

Alguém sabe como posso capturar voz por um acaso terei de usar o JavaSpeach?? agora e para fazer o envio dos pacotes?

Valeu
Alexandro

4 Respostas

W

Intaum … eu tb to querendo fazer um programa assim …
ainda to pesquisando , assim q tiver mais informacoes vou postar aki !!!

faloww ae t+

M

Pessoal, eu já fiz um desses a muito tempo atrás …

Taí o codigo :

/**
 * @author Marcio L. M.
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */



import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

public class Transmissor
{

	private static int porta;	
	private String host;
	private AudioFormat formato;
	private Socket socket;
	private DataOutputStream saida;
	TargetDataLine line;

	public Transmissor( String host )
	{

		this.host = host;
		formato = new AudioFormat( AudioFormat.Encoding.PCM_UNSIGNED, 
					   (float)8000, 8, 1, 1, (float)8000, false );

	}
	

	public void executar()
	{
		
		try
		{	
			
                        socket = new Socket("127.0.0.1", porta );
			saida = new DataOutputStream( socket.getOutputStream() );
			
			DataLine.Info info;
			info = new DataLine.Info( TargetDataLine.class, formato );
			line = (TargetDataLine) AudioSystem.getLine( info );
			line.open( formato, line.getBufferSize() );

                        int frameSizeInBytes = formato.getFrameSize();
			int bufferLengthInFrames = line.getBufferSize() / 8;
			int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
			byte[] data = new byte[ bufferLengthInBytes ];

                        System.out.println("Formato           = " + formato.toString() );
                        System.out.println("Taxa              = " + formato.getSampleRate() );
			System.out.println("Bytes por Frame   = " + frameSizeInBytes );
			System.out.println("tamanho do Buffer = " + line.getBufferSize()  );
			System.out.println("Frames por Buffer = " + bufferLengthInFrames );
			System.out.println("Buffers em Frames = " + bufferLengthInBytes );

			line.start();

			System.out.println("....Conectado ao Servidor");
			System.out.println("....Pronto para Transmissão!");
			int cont=0;
			
                        while ( line.read( data, 0, data.length ) != -1 ) 
                        {       
				saida.write( data );

                        }

			line.stop();
			line.close();

			saida.close();
			socket.close();

		} 
		catch ( SocketException e ) 
		{

			return;

		} 
		catch ( Exception e ) 
		{

			e.printStackTrace();

		} 
		finally 
		{

			System.out.println("Fim da Transmissao...!");
			fecharSocket();

		}

	}

	private void fecharSocket() 
	{
		
		try 
		{

			if ( socket != null ) 
			{

				socket.close();
				line.close();
				saida.close();

			}

		} 
		catch ( IOException e ) 
		{

			System.out.println("Erro ao fechar a conexao!");
			e.printStackTrace();

		}
	}

	public static void main( String[] args ) 
	{

		if ( args.length == 0 ) 
		{
			System.out.println("É necessário específicar uma porta válida!");
			System.exit( 0 );
		}
		Transmissor trasmissor = new Transmissor( args[ 0 ] );
		porta = Integer.parseInt( args[ 0 ] );
		trasmissor.executar();
	}


}
/**
 * @author Marcio L. M
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */



import java.net.*;
import java.io.*;
import javax.sound.sampled.*;

public class Receptor
{
	
	private File file;
	private Socket socket;
	private static int porta;
	private SourceDataLine line;
	private AudioFormat formato;
	private int frameSizeInBytes;
	private DataInputStream entrada;
	private int bufferLengthInBytes;
	
	public Receptor()
	{

		formato = new AudioFormat( AudioFormat.Encoding.PCM_UNSIGNED, 
					   (float)8000, 8, 1, 1, (float)8000, false );

	}

	public void executar()
	{
		ServerSocket serverSocket = null;
		
		try
		{

			serverSocket = new ServerSocket( porta );
			System.out.println("Receptor Ativado na Porta : " + porta );

		}
		catch( Exception e )
		{
			
			System.out.println("Erro ao conectar " + e.toString() );
                        e.printStackTrace();
			fecharSocket();
			return;

		}
		
		while( true )
		{
			
			try
			{
				
				socket = serverSocket.accept();
				System.out.println("Aguardando conexao ... " + socket.toString() );
				
				entrada = new DataInputStream( socket.getInputStream() );
				
				DataLine.Info info;
				info = new DataLine.Info( SourceDataLine.class, formato );
				SourceDataLine line = (SourceDataLine) AudioSystem.getLine( info );
				line.open( formato, line.getBufferSize() );
				
                        frameSizeInBytes = formato.getFrameSize();
				int bufferLengthInFrames = line.getBufferSize() / 8;
				bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
				byte[] data = new byte[ bufferLengthInBytes ];

                        System.out.println("Formato          = " + formato.toString());
                        System.out.println("Taxa             = " + formato.getSampleRate());
				System.out.println("Bytes por Frame  = " + frameSizeInBytes );
				System.out.println("Tamnho do Buffer = " + line.getBufferSize() );
				System.out.println("Buffer em Frames = " + bufferLengthInFrames );
				System.out.println("Buffers em Bytes = " + bufferLengthInBytes );
				
				line.start();
				
                        while ( entrada.read( data ) != -1 ) 
                        {
					
					line.write( data, 0, data.length );

                        }
				
				line.drain();
				line.stop();
				line.close();
				
				entrada.close();
				socket.close();
				System.out.println("Modulo Receptor Fechado");

			}
			catch ( IOException e ) 
			{
				e.printStackTrace();

			} 
			catch ( Exception e ) 
			{
				e.printStackTrace();

			} 
			finally 
			{
				fecharSocket();
				System.out.println("Receptor Desativado!");

			}
		}

	
	}

	private void fecharSocket() 
	{
		try 
		{ 
			if ( socket != null ) 
			{
				socket.close();
			}
		} 
		catch ( IOException e ) 
		{
			
			System.out.println("Erro ao fechar a conexao!");
			e.printStackTrace();
			
		}
	}

	public static void main( String[] args ) 
	{

		if ( args.length == 0 ) 
		{
			System.out.println("É necessário específicar uma porta válida!");
			System.exit( 0 );
		}

		porta = Integer.parseInt( args[ 0 ] );
		Receptor receptor = new Receptor();
		receptor.executar();
	}
			
}

Não pensem que está tudo pronto, ainda tem muitos problemas para ser resolvidos neste código, como por exemplo o delay que ele gera ao londo da transmissão.
Bom…
Já é um começo para vocês, agora falta pesquisar mais um pouco para aperfeiçoar.

[]'s

A

Márcio, valeu msmo meu!

Vou dar uma olhada, acho q terei apenas que adaptá-lo para as redes P2P ou nem isso, irá servir como uma BOA base…

e sobre o JavaSpeach? terei q usá-lo, você não utilizou isso

Valeu
Alexandro

M

Alexandro, no meu caso eu apenas passei um programa que enviava texto para enviar voz uitilizando um socket.
Não tenho muitos conhecimentos sobre essa área. Esse aí eu fiz naqueles dias que a gente não tem nada na mente e começa a invertar…Risos… Surgiu do nada.
Quanto oa Java Speach eu sei muitissimo pouco sobre essa ele. Somente lí uma vez que ele funciona bem na interpretação de voz.
Se não me engano na Revista Java Magazine ed. 4 ano I.
Ok…
Procure por esta revista ou código fonte associdado a ela.
Boa sorte em seu projeto!

[]'s

Criado 30 de março de 2005
Ultima resposta 31 de mar. de 2005
Respostas 4
Participantes 3