Skip to content

Commit

Permalink
Merge pull request #11 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 authored Nov 1, 2022
2 parents 4b50ecd + 4183d40 commit 4e9a682
Show file tree
Hide file tree
Showing 24 changed files with 665 additions and 79 deletions.
4 changes: 2 additions & 2 deletions OmgPianoTranscription/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea
transcription.onnx
out.mid
target
target
/pianotranscriptioncli/src/main/resources/output/
4 changes: 3 additions & 1 deletion OmgPianoTranscription/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
+ **FFMPEG** is required, please [install](https://www.gyan.dev/ffmpeg/builds/) it in PATH.
+ Download [**ONNX file**](https://github.com/EveElseIf/pianotranscription_java/releases/download/blob/transcription.onnx) and put it on the root of Folder`pianotranscriptioncli/src/main/resources`.
+ Modify the input audio file path in the main function of `pianotranscriptioncli/src/main/java/pianotranscriptioncli/Program.java`
+ Build and run the project, and you'll find the output midi file in Folder`pianotranscriptioncli/src/main/resources/output`.
+ Well, if your running system is not Windows, you need to change the string of the file path of the input and output files to make it work.
+ Build and run the project, feel free to run the `test.http` file in the resource folder if you use IDEA as the IDE, and you'll find the output midi file in Folder`pianotranscriptioncli/src/main/resources/output`. By the way, you can see the process on the command lines in your terminal.
+ 🎉 Go bears! Just enjoy it!
17 changes: 17 additions & 0 deletions OmgPianoTranscription/pianotranscriptioncli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,22 @@
<artifactId>libpianotranscription</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.pianotranscriptioncli;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TranscriptionApplication {
public static void main(String[] args) {
// 注:这里传入的字段码对象,必需是声明了@SpringBootApplication的类
//启动SpringBoot程序
SpringApplication.run(TranscriptionApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.pianotranscriptioncli.common.api;

import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
* 通用返回对象
*
*/
@Getter
@Setter
public class CommonResult {
private long code;
private String message;
private Object data;

protected CommonResult() {
}

protected CommonResult(long code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
}

/**
* 成功返回结果
*
*/
public static CommonResult success() {
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
}

/**
* 成功返回结果
*
* @param data 获取的数据
*/
public static CommonResult success(Object data) {
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
}

/**
* 成功返回结果
*
* @param data 获取的数据
* @param message 提示信息
*/
public static CommonResult success(Object data, String message) {
return new CommonResult(ResultCode.SUCCESS.getCode(), message, data);
}

/**
* 失败返回结果
*
* @param errorCode 错误码
*/
public static CommonResult failed(ErrorCode errorCode) {
return new CommonResult(errorCode.getCode(), errorCode.getMessage(), null);
}

/**
* 失败返回结果
*
* @param errorCode 错误码
* @param message 错误信息
*/
public static CommonResult failed(ErrorCode errorCode, String message) {
return new CommonResult(errorCode.getCode(), message, null);
}

/**
* 失败返回结果
*
* @param message 提示信息
*/
public static CommonResult failed(String message) {
return new CommonResult(ResultCode.FAILED.getCode(), message, null);
}

/**
* 失败返回结果
*/
public static CommonResult failed() {
return failed(ResultCode.FAILED);
}

/**
* 参数验证失败返回结果
*/
public static CommonResult validateFailed() {
return failed(ResultCode.VALIDATE_FAILED);
}

/**
* 参数验证失败返回结果
*
* @param message 提示信息
*/
public static CommonResult validateFailed(String message) {
return new CommonResult(ResultCode.VALIDATE_FAILED.getCode(), message, null);
}

/**
* 未登录返回结果
*/
public static CommonResult unauthorized(Object data) {
return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
}

/**
* 未授权返回结果
*/
public static CommonResult forbidden(Object data) {
return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
}

/**
* 通过map携带简单键值对数据的无限链式调用,但切记不要在data已经不是map类型后调用
*/
public CommonResult with(String k, Object v) {
Map<String, Object> map;
if (data == null) {
map = new HashMap<>();
} else {
map = (Map<String, Object>) this.data;
}
map.put(k, v);
this.data = map;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.pianotranscriptioncli.common.api;

/**
* 封装API的错误码
*
*/
public interface ErrorCode {
long getCode();

String getMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.pianotranscriptioncli.common.api;

public enum ResultCode implements ErrorCode {

SUCCESS(1, "操作成功"),
FAILED(-1, "操作失败"),
VALIDATE_FAILED(101, "参数检验失败"),
UNAUTHORIZED(102, "暂未登录或token已经过期"),
FORBIDDEN(103, "没有相关权限");

private final long code;
private final String message;

ResultCode(long code, String message) {
this.code = code;
this.message = message;
}

@Override
public long getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.pianotranscriptioncli.common.exception;

import com.pianotranscriptioncli.common.api.ErrorCode;

public class Asserts extends org.springframework.util.Assert {
public static void fail(String message) {
throw new BaseException(message);
}

public static void fail(ErrorCode errorCode) {
throw new BaseException(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.pianotranscriptioncli.common.exception;

import com.pianotranscriptioncli.common.api.ErrorCode;
import lombok.Getter;
import lombok.Setter;

/**
*
* 自定义API异常
*/
@Getter
@Setter
public class BaseException extends RuntimeException{
private ErrorCode errorCode;

public BaseException() {
super();
}

public BaseException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}

public BaseException(String message) {
super(message);
}

public BaseException(Throwable cause) {
super(cause);
}

public BaseException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.pianotranscriptioncli.controller;

import com.pianotranscriptioncli.common.api.CommonResult;
import com.pianotranscriptioncli.dto.Mp3ImportDTO;
import com.pianotranscriptioncli.dto.Mp3ImportWithFileDTO;
import com.pianotranscriptioncli.service.TranscriptionService;
import com.pianotranscriptioncli.service.impl.TranscriptionServiceImpl;
import com.pianotranscriptioncli.vo.Mp3ImportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("/transcription")
public class TranscriptionController {

@Autowired
TranscriptionService transcriptionService;

@PostMapping(value = "/fuck")
public String Mp3TOMidiUpload() throws Exception {
return "hello";
}

@PostMapping(value = "/mp3ToMidi", consumes = {"application/json"})
@ResponseBody
public Mp3ImportVO Mp3ToMidi(@RequestBody Mp3ImportDTO mp3ImportDTO) throws Exception {
try {
CommonResult commonResult = transcriptionService.Mp3TOMidiUpload(mp3ImportDTO);
if (commonResult.getCode() == 1) {
return new Mp3ImportVO(true, commonResult.getData().toString(), null);
} else {
return new Mp3ImportVO(false, null, commonResult.getMessage());
}
} catch (NullPointerException e) {
return new Mp3ImportVO(false, null, "请检查是否传入了正确的参数");
}
}


@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);
try {
CommonResult commonResult = transcriptionService.Mp3TOMidiUploadWithFile(mp3ImportWithFileDTO);
if (commonResult.getCode() == 1) {
return new Mp3ImportVO(true, commonResult.getData().toString(), null);
} else {
return new Mp3ImportVO(false, null, commonResult.getMessage());
}
} catch (NullPointerException e) {
return new Mp3ImportVO(false, null, "请检查是否传入了正确的参数");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pianotranscriptioncli.dto;

import lombok.Data;
import lombok.NonNull;

@Data
public class Mp3ImportDTO {
@NonNull
private boolean isAbsolute;
@NonNull
private String resourcePath;
@NonNull
private String songName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.pianotranscriptioncli.dto;

import lombok.Data;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.springframework.web.multipart.MultipartFile;

@Data
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) {
this.file = file;
this.outPath = outPath;
this.songName = songName;
}

}

Loading

0 comments on commit 4e9a682

Please sign in to comment.