Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ru.bmstu;
- import java.nio.charset.StandardCharsets;
- import org.apache.commons.io.IOUtils;
- import sun.audio.*;
- import java.io.PipedOutputStream;
- import java.io.PipedInputStream;
- import java.io.DataInputStream;
- import java.io.PrintStream;
- import java.io.File;
- import java.awt.Image;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.lang.String;
- import java.net.ConnectException;
- import java.sql.Blob;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.naming.NamingException;
- import java.io.BufferedInputStream;
- import javazoom.jl.player.Player;
- import javax.sound.sampled.*; //AudioSystem;
- import java.io.InputStream;
- import java.io.IOException;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.DataLine;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.SourceDataLine;
- import java.io.FilterInputStream;
- import javax.ws.rs.core.Response;
- import java.sql.DriverManager;
- import org.apache.commons.io.IOUtils;
- import javax.servlet.http.*;
- import java.io.OutputStream;
- /**
- *
- * @author Dell
- */
- public class AnFilter {
- Connection conn;
- private long _blobId;
- private long retNum;
- private String MimeType;
- private String fileName;
- private boolean _controlErrors;
- private InputStream isFile;
- private String outFile;
- private InputStream filteredFile;
- private int size;
- //public static void main(String[] args) {
- //String part1 = args[0];
- //String part2 = args[1];
- //int songID=Integer.parseInt(part1);
- //int methNum=Integer.parseInt(part2);
- //int songID=150; //только для тестирования типо
- //int methNum=1; //только для тестирования типо
- /*Filter fil = new Filter(songID);
- try{
- System.out.println("Устанавливаем соединение...");
- fil.initConnection();
- System.out.println("Выгружаем данные...");
- fil.initFile();
- System.out.print("Название композиции:");
- System.out.println(fil.getFileName());
- if (methNum==1)
- {
- System.out.println("Добавляем шум...");
- SimpleSoundPlayer sound = new SimpleSoundPlayer(fil.getIsFile());
- InputStream is = new ByteArrayInputStream(sound.getSamples());
- EchoFilter filter = new EchoFilter(11025, .001f);
- is = new FilteredSoundStream(is, filter);
- System.out.println("Загружаем обработанные данные...");
- fil.toDataBase(fil.getFileName(),is);
- }
- else
- {
- System.out.println("пока нифига");
- }
- System.out.println("Закрываем соединение...");
- fil.getConnection().close();
- }
- catch(Exception ex){
- ex.printStackTrace();
- }*/
- //}
- AnFilter(int syncID){
- _blobId=syncID;
- MimeType="audio/aiff";
- size=0;
- }
- public void setFilteredFile(InputStream is){
- filteredFile=is;
- }
- public InputStream getFilteredFile(){
- return filteredFile;
- }
- public String getMimeType(){
- return MimeType;
- }
- public String getFileName(){
- return fileName;
- }
- public long getRetNum(){
- return retNum;
- }
- public InputStream getIsFile(){
- return isFile;
- }
- public InputStream getfilteredFile(){
- return filteredFile;
- }
- public Connection getConnection(){
- return conn;
- }
- public void initConnection() throws SQLException, NamingException, ClassNotFoundException {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection(
- "jdbc:oracle:thin:@localhost:1521:ORCL", "Mokona",
- "Mokona");
- conn.setAutoCommit(false);
- }
- public void initFile() throws SQLException {
- boolean wasQuery;
- try (PreparedStatement ps = conn.prepareStatement("select t.blob_data, t.file_name from audio t where t.sync_id = ?")) {
- ResultSet rs;
- Blob blob;
- wasQuery = false;
- while(!wasQuery && _blobId < 10000){
- ps.setLong(1, _blobId);
- rs = ps.executeQuery();
- System.out.println(ps.toString());
- while (rs.next()) {
- blob = rs.getBlob(1);
- isFile = blob.getBinaryStream();
- fileName = rs.getString(2);
- wasQuery = true;
- }
- rs.close();
- }
- }
- if (!wasQuery) {
- throw new RuntimeException("Строка с sync_id=" + Long.toString(_blobId) + " не найдена.");
- }
- //return fileName;
- }
- public static void copyStream(InputStream input, OutputStream output)
- throws IOException
- {
- byte[] buffer = new byte[1024]; // Adjust if you want
- int bytesRead;
- while ((bytesRead = input.read(buffer)) != -1)
- {
- output.write(buffer, 0, bytesRead);
- }
- }
- public static void copyReverseStream(InputStream input, OutputStream output, int size)
- throws IOException
- {
- byte[] buffer = new byte[1024]; // Adjust if you want
- int bytesRead;
- int i=0;
- while ((bytesRead = input.read(buffer)) != -1)
- {
- output.write(buffer, size-i, bytesRead);
- i++;
- }
- }
- public void toDataBase(String name, InputStream is) throws SQLException, IOException {
- try {
- String Query;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- copyStream(is,baos);
- //copyReverseStream(is,baos,700);
- size=baos.size();
- System.out.print("Размер файла:");
- System.out.println(size);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
- Query = "declare ";
- Query+= "nSYNC_NEW number; ";
- Query+= "begin ";
- Query+= "nSYNC_NEW:=seq_id.nextval ; ";
- Query+= " insert into audio (sync_id, file_name, blob_data, mime_type) "
- + "values(nSYNC_NEW, ?, ?, 'audio/aiff'); ";
- Query+= " ? := nSYNC_NEW; ";
- Query+= " end;";
- CallableStatement cs = conn.prepareCall(Query);
- name="'shit_"+name+"'";
- cs.setString(1, name);
- cs.setBlob(2,bais,size);
- cs.registerOutParameter(3, java.sql.Types.NUMERIC);
- cs.executeUpdate();
- long nSYNC_NEW = cs.getLong(3);
- System.out.print("ID обработанной композиции:");
- System.out.println(nSYNC_NEW);
- retNum = nSYNC_NEW;
- cs.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally {
- conn.close();
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ru.bmstu;
- import java.io.ByteArrayInputStream;
- import java.io.InputStream;
- import javax.jws.WebService;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import java.lang.*;
- import java.nio.ByteBuffer;
- import org.apache.commons.io.IOUtils;
- import ru.bmstu.Filter;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.DataLine;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.SourceDataLine;
- /**
- *
- * @author Dell
- */
- @WebService(serviceName = "NewWebService")
- public class NewWebService {
- public final int BUFFER_SIZE = 131072;
- byte [] buff;
- short [] sampleBuff;
- /**
- * This is a sample web service operation
- */
- @WebMethod(operationName = "hello")
- public long hello(@WebParam(name = "songID") int songID,
- @WebParam(name = "methNum") int methNum) {
- AnFilter fil = new AnFilter(songID);
- try{
- System.out.println("Устанавливаем соединение...");
- fil.initConnection();
- System.out.println("Выгружаем данные...");
- fil.initFile();
- System.out.print("Название композиции:");
- System.out.println(fil.getFileName());
- if (methNum==1)
- {
- System.out.println("Добавляем эффект...");
- SimpleSoundPlayer sound = new SimpleSoundPlayer(fil.getIsFile());
- InputStream is = new ByteArrayInputStream(sound.getSamples());
- EchoFilter filter = new EchoFilter(11025, .01f);
- is = new FilteredSoundStream(is, filter);
- //sound.play(is);
- /*try {
- AudioInputStream audioStream = AudioSystem.getAudioInputStream(fil.getIsFile());
- AudioFormat audioFormat = audioStream.getFormat();
- DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
- SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);
- sourceLine.open(audioFormat);
- sourceLine.start();
- int nBytesRead = 0;
- buff = new byte[BUFFER_SIZE];
- sampleBuff = new short[BUFFER_SIZE>>>1];
- System.out.println(BUFFER_SIZE>>>1);
- Equalizer eq = new Equalizer(BUFFER_SIZE>>>1);
- while (nBytesRead != -1) {
- nBytesRead = audioStream.read(buff, 0, buff.length);
- //System.out.println(nBytesRead);
- sampleBuff = new short[nBytesRead/2];
- this.ByteArrayToSamplesArray();
- eq.setInputSignal(sampleBuff);
- eq.equalization();
- this.SampleArrayByteArray();
- if (nBytesRead >= 0) {
- @SuppressWarnings("unused")
- int nBytesWritten = sourceLine.write(buff, 0, nBytesRead);
- }
- }
- sourceLine.drain();
- sourceLine.close();
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(1);
- }*/
- System.out.println("Загружаем обработанные данные...");
- fil.toDataBase(fil.getFileName(),fil.getIsFile());
- }
- else
- {
- System.out.println("Загружаем обработанные данные...");
- fil.toDataBase(fil.getFileName(),fil.getIsFile());
- }
- System.out.println("Закрываем соединение...");
- fil.getConnection().close();
- }
- catch(Exception ex){
- ex.printStackTrace();
- }
- return fil.getRetNum();
- }
- private void SampleArrayByteArray() {
- for(int i = 0, j = 0; i < this.sampleBuff.length && j < (this.buff.length); i++, j += 2 ) {
- this.buff[j] = (byte)(this.sampleBuff[i]);
- this.buff[j + 1] = (byte)(this.sampleBuff[i] >>> 8);
- }
- }
- private void ByteArrayToSamplesArray() {
- if (buff.length < BUFFER_SIZE) return;
- for(int i = 0, j = 0; i < this.buff.length - 2; i += 2 , j++) {
- this.sampleBuff[j] = (short) (0.5 * (ByteBuffer.wrap(this.buff, i, 2).order(
- java.nio.ByteOrder.LITTLE_ENDIAN).getShort()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement