Skip to content

Commit

Permalink
Merge pull request #17 from SleepyLGod/fix-midi-generator-cli
Browse files Browse the repository at this point in the history
Fix midi generator cli
  • Loading branch information
SleepyLGod committed Nov 10, 2022
2 parents aea03e4 + fe59d10 commit 7653e07
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

Expand Down Expand Up @@ -45,19 +46,21 @@ public Mp3ImportVO Mp3ToMidi(@RequestBody Mp3ImportDTO mp3ImportDTO) throws Exce

@ResponseBody
@PostMapping(value = "/mp3ToMidiWithFile", consumes = {"multipart/form-data"})
public Mp3ImportVO Mp3ToMidiWithFile(@RequestParam("file")MultipartFile file,
@RequestParam("outPath")String outPath,
@RequestParam("songName")String songName) throws Exception {
Mp3ImportWithFileDTO mp3ImportWithFileDTO = new Mp3ImportWithFileDTO(file, outPath, songName);
public void Mp3ToMidiWithFile(@RequestParam("file")MultipartFile file,
// @RequestParam("outPath")String outPath,
@RequestParam("songName")String songName,
HttpServletResponse response) throws Exception {
Mp3ImportWithFileDTO mp3ImportWithFileDTO = new Mp3ImportWithFileDTO(file, songName);
try {
CommonResult commonResult = transcriptionService.Mp3TOMidiUploadWithFile(mp3ImportWithFileDTO);
CommonResult commonResult = transcriptionService.Mp3TOMidiUploadWithFile(mp3ImportWithFileDTO, response);
if (commonResult.getCode() == 1) {
return new Mp3ImportVO(true, commonResult.getData().toString(), null);
System.out.println("success");
} else {
return new Mp3ImportVO(false, null, commonResult.getMessage());
System.out.println("fail");
}
} catch (NullPointerException e) {
return new Mp3ImportVO(false, null, "请检查是否传入了正确的参数");
System.out.println("fail");
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public class Mp3ImportDTO {
@NonNull
private String resourcePath;
@NonNull
private String outPath;
@NonNull
private String songName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ public class Mp3ImportWithFileDTO {
@NonNull
private MultipartFile file;
@NonNull
private String outPath;
@NonNull
private String songName;
@NonNull
private String inputPath = "D:\\gitrepositories\\omg-score\\OmgPianoTranscription\\pianotranscriptioncli\\src\\main\\resources\\";

public Mp3ImportWithFileDTO(MultipartFile file, String outPath, String songName) {
@NonNull
private String outPath = "D:\\gitrepositories\\omg-score\\OmgPianoTranscription\\pianotranscriptioncli\\src\\main\\resources\\output\\";
public Mp3ImportWithFileDTO(MultipartFile file, String songName) {
this.file = file;
this.outPath = outPath;
this.songName = songName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.pianotranscriptioncli.dto.Mp3ImportDTO;
import com.pianotranscriptioncli.dto.Mp3ImportWithFileDTO;

import javax.servlet.http.HttpServletResponse;

public interface TranscriptionService {
CommonResult Mp3TOMidiUpload(Mp3ImportDTO mp3ImportDTO) throws Exception;

CommonResult Mp3TOMidiUploadWithFile(Mp3ImportWithFileDTO mp3ImportWithFileDTO) throws Exception;
CommonResult Mp3TOMidiUploadWithFile(Mp3ImportWithFileDTO mp3ImportWithFileDTO, HttpServletResponse response) throws Exception;

String WavToMidiUpload();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import com.pianotranscriptioncli.dto.Mp3ImportWithFileDTO;
import com.pianotranscriptioncli.service.TranscriptionService;
import com.pianotranscriptioncli.utils.Utils;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@Service
public class TranscriptionServiceImpl implements TranscriptionService {
Expand All @@ -23,25 +28,32 @@ public CommonResult Mp3TOMidiUpload(Mp3ImportDTO mp3ImportDTO) throws Exception
System.out.println(System.getProperty("user.dir"));
resourcePath = System.getProperty("user.dir") + mp3ImportDTO.getResourcePath(); // "\\src\\main\\resources\\"
}
//String ans = resourcePath + "output\\" + mp3ImportDTO.getSongName() + ".mid";
String ans = Utils.Convertor(resourcePath, mp3ImportDTO.getSongName());
String outPath = mp3ImportDTO.getOutPath() + mp3ImportDTO.getSongName() + ".mid";
String ans = Utils.ConvertorRedirect(resourcePath, mp3ImportDTO.getSongName(), outPath);
if (ans != null) {
return CommonResult.success(ans, "mp3转换成功");
} else {
return CommonResult.failed("mp3转换失败");
}
}

/**
* 上传文件
* @param mp3ImportWithFileDTO
* @param response
* @return CommonResult
* @throws Exception
*/
@Override
public CommonResult Mp3TOMidiUploadWithFile(Mp3ImportWithFileDTO mp3ImportWithFileDTO) throws Exception {
public CommonResult Mp3TOMidiUploadWithFile(Mp3ImportWithFileDTO mp3ImportWithFileDTO, HttpServletResponse response) throws Exception {
MultipartFile file = mp3ImportWithFileDTO.getFile();
String inputFilePath;
if (!file.isEmpty()) {
/*
/*
String fileName = file.getOriginalFilename(); // 获取文件名
assert fileName != null;
String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 获取文件的后缀名
*/
*/
inputFilePath = mp3ImportWithFileDTO.getInputPath() + "input\\" + mp3ImportWithFileDTO.getSongName() + ".mp3";
File dest = new File(inputFilePath);
if (!dest.getParentFile().exists()) { // 检测是否存在目录
Expand All @@ -65,8 +77,43 @@ public CommonResult Mp3TOMidiUploadWithFile(Mp3ImportWithFileDTO mp3ImportWithFi

String outPath = mp3ImportWithFileDTO.getOutPath() + mp3ImportWithFileDTO.getSongName() + ".mid";
String output = Utils.ConvertorRedirect(mp3ImportWithFileDTO.getInputPath(), mp3ImportWithFileDTO.getSongName(), outPath);
OutputStream outputStream = null;
if (output != null) {
return CommonResult.success(output, "mp3转换成功");
File outputFile = new File(output);
if (!outputFile.exists()) {
throw new Exception("midi文件不存在");
}
// System.out.println(outputFile);
try {
// 通过response返回
// 设置文件头 (URLEncoder.encode(mp3ImportWithFileDTO.getSongName() + ".mid", StandardCharsets.US_ASCII)))
response.setHeader("Content-Disposition", "attchement;filename=" + URLEncoder.encode(mp3ImportWithFileDTO.getSongName() + ".mid", StandardCharsets.UTF_8));
response.setCharacterEncoding("UTF-8");
response.setContentType("audio/mid");
// response.setContentType("application/octet-stream");
InputStream fis = new BufferedInputStream(new FileInputStream(outputFile));
byte[] buffer = new byte[fis.available()];
// fis.read(buffer);
fis.close();
response.reset();
outputStream = new BufferedOutputStream(response.getOutputStream());
System.out.println(Arrays.toString(buffer));
outputStream.write(buffer);
System.out.println(outputStream);
outputStream.flush();
// IOUtils.copy(fis, outputStream);
response.flushBuffer();
} catch (Exception e) {
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
System.out.println(response.getOutputStream());
return CommonResult.success(response.getClass(), "mp3转换成功");
} else {
return CommonResult.failed("mp3转换失败");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.pianotranscriptioncli.utils;

import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;

public class MidiUtils {

/**
* 读取并播放midi文件
* &#064;参考文档 <a href="https://docs.oracle.com/javase/7/docs/api/javax/sound/midi/package-summary.html">...</a>
* @param output 完整路径(带后缀)
*/
public static void Reduce(String output) {
try {
Sequence sequence = MidiSystem.getSequence(new File(output));
long length = sequence.getMicrosecondLength(); // 获取序列的总时间(微秒)
int trackCount = sequence.getTracks().length; // 获取序列的音轨数
float divType = sequence.getDivisionType(); // 获取序列的(计时方式?)
int resolution = sequence.getResolution(); // 获取序列的时间解析度

MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); // 获取所有 midi 设备的信息
Sequencer sequencer = MidiSystem.getSequencer(); // 获取默认的音序器
Synthesizer synthsizer = MidiSystem.getSynthesizer(); // 获取默认的合成器
Receiver receiver = MidiSystem.getReceiver(); // 获取默认的接收器
Transmitter transmitter = MidiSystem.getTransmitter(); // 获取默认的传输器
if(sequencer == null) {
throw new IOException("未找到可用音序器!");
}
sequencer.setSequence(sequence); // 设置midi序列

sequencer.start(); // 开始播放当前序列

sequencer.stop(); // 停止播放当前序列

// sequencer.setTempoFactor(float factor); // 设置速度比率 (1.0f 为原速)
//
// sequencer.setMicrosecondPosition(long microseconds); // 设置播放位置到指定微秒
//
// sequencer.setTrackMute(int track, boolean mute); // 开启或关闭一条音轨的静音模式
//
// sequencer.setTrackSolo(int track, boolean solo); // 开启或关闭一条音轨的独奏模式

} catch (InvalidMidiDataException | IOException | MidiUnavailableException e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.pianotranscriptioncli.utils;

import ai.onnxruntime.OrtException;
import libpianotranscription.Transcriptor;

import javax.sound.midi.InvalidMidiDataException;
import java.io.*;
import java.nio.file.Files;

Expand Down Expand Up @@ -30,6 +32,10 @@ public static String Convertor(String resourcePath, String songName) throws Exce
String outPutFilePath = resourcePath + "output\\" + songName + ".mid";

preProcessFile(inputFilePath);
return getString(resourcePath, outPutFilePath);
}

private static String getString(String resourcePath, String outPutFilePath) throws IOException, OrtException, InvalidMidiDataException {
byte[] a = Files.readAllBytes(new File("test.pcm").toPath());
var b = Utils.normalizeShort(Utils.toShortLE(a));
try {
Expand All @@ -52,27 +58,8 @@ public static String Convertor(String resourcePath, String songName) throws Exce
}

public static String ConvertorRedirect(String resourcePath, String songName, String outputPath) throws Exception {

preProcessFile(resourcePath + "input\\" + songName + ".mp3");
byte[] a = Files.readAllBytes(new File("test.pcm").toPath());
var b = Utils.normalizeShort(Utils.toShortLE(a));
try {
var ans = new File("test.pcm").delete();
} catch (Exception e) {
e.printStackTrace();
}

var transcriptor = new Transcriptor(resourcePath + "transcription.onnx");
var out = transcriptor.transcript(b);
try(var file = new FileOutputStream(outputPath)) {
file.write(out);
System.out.println("OK");
// System.exit(0);
return outputPath;
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
return null;
return getString(resourcePath, outputPath);
}

private static void preProcessFile(String fileName) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
### Successful test: check response status is 200
GET https://httpbin.org/status/200

> {%
client.test("Request executed successfully", function() {
client.assert(response.status === 200, "Response status is not 200");
});
%}

### Failed test: check response status is 200
GET https://httpbin.org/status/404

> {%
client.test("Request executed successfully", function() {
client.assert(response.status === 200, "Response status is not 200");
});
%}

### Check response status and content-type
GET https://httpbin.org/get

> {%
client.test("Request executed successfully", function() {
client.assert(response.status === 200, "Response status is not 200");
});

client.test("Response content-type is json", function() {
var type = response.contentType.mimeType;
client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'");
});
%}

### Check response body
GET https://httpbin.org/get

> {%
client.test("Headers option exists", function() {
client.assert(response.body.hasOwnProperty("headers"), "Cannot find 'headers' option in response");
});
%}


### Send POST request with json body
POST https://httpbin.org/post
Content-Type: application/json

{
"id": 999,
"value": "content"
}

### Send POST request with body as parameters
POST https://httpbin.org/post
Content-Type: application/x-www-form-urlencoded

id=999&value=content

### Send a form with the text and file fields
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="element-name"
Content-Type: text/plain

Name
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data.json"
Content-Type: application/json

< ./request-form-data.json
--WebAppBoundary--

### Send request with dynamic variables in request's body
POST https://httpbin.org/post
Content-Type: application/json

{
"id": {{$uuid}},
"price": {{$randomInt}},
"ts": {{$timestamp}},
"value": "content"
}

### Basic authorization.
GET https://httpbin.org/basic-auth/user/passwd
Authorization: Basic user passwd

### Basic authorization with variables.
GET https://httpbin.org/basic-auth/user/passwd
Authorization: Basic {{username}} {{password}}

### Digest authorization.
GET https://httpbin.org/digest-auth/realm/user/passwd
Authorization: Digest user passwd

### Digest authorization with variables.
GET https://httpbin.org/digest-auth/realm/user/passwd
Authorization: Digest {{username}} {{password}}

### Authorization by token, part 1. Retrieve and save token.
POST https://httpbin.org/post
Content-Type: application/json

{
"token": "my-secret-token"
}

> {% client.global.set("auth_token", response.body.json.token); %}

### Authorization by token, part 2. Use token to authorize.
GET https://httpbin.org/headers
Authorization: Bearer {{auth_token}}

###
Loading

0 comments on commit 7653e07

Please sign in to comment.