Skip to content

Commit

Permalink
本地代码剩余提交
Browse files Browse the repository at this point in the history
  • Loading branch information
cph committed Oct 10, 2024
1 parent a7420f5 commit ee333d6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
14 changes: 8 additions & 6 deletions chrome-plugin-easemusic/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
position: fixed;
top: 10%;
right: 0;
width: 25%;
height: 90%;
width: 15%;
height: 50%;
background: white;
border: 1px solid #ccc;
z-index: 9999;
display: none;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
overflow: hidden;
pointer-events: auto;
}
Expand Down Expand Up @@ -42,7 +42,8 @@
cursor: pointer;
font-size: 20px;
transition: background-color 0.3s;
flex: 1; /* 让按钮平均分配空间 */
flex: 1;
/* 让按钮平均分配空间 */
}

#ease-music-player button:hover {
Expand All @@ -57,7 +58,8 @@

/* 如果需要,可以为关闭按钮添加特定样式 */
#ease-music-close {
background-color: #f44336; /* 红色背景 */
background-color: #f44336;
/* 红色背景 */
}

#ease-music-close:hover {
Expand All @@ -69,4 +71,4 @@
background-color: #f0f0f0;
padding: 2.5px;
font-size: 7px;
}
}
67 changes: 34 additions & 33 deletions chrome-plugin-easemusic/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,17 @@ function toggleRecording() {
}

function startRecording() {
console.log('startRecording function called'); // 添加这行来调试
console.log('startRecording function called');
audioChunks = [];

// 创建一个新的 AudioContext,设置采样率为 16000Hz
const audioContext = new (window.AudioContext || window.webkitAudioContext)({
sampleRate: 16000
});

// 创建一个 MediaStreamDestination
const destination = audioContext.createMediaStreamDestination();

// 获取页面中所有的 audio 和 video 元素
const mediaElements = document.querySelectorAll('audio, video');
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
const audioContext = new (window.AudioContext || window.webkitAudioContext)({
sampleRate: 16000
});

mediaElements.forEach(element => {
if (element.captureStream) {
// 捕获媒体元素的音频流
const stream = element.captureStream();
const source = audioContext.createMediaStreamSource(stream);
const destination = audioContext.createMediaStreamDestination();

// 创建一个 ScriptProcessorNode 用于重采样
const bufferSize = 4096;
Expand Down Expand Up @@ -107,26 +99,26 @@ function startRecording() {

source.connect(scriptNode);
scriptNode.connect(destination);
}
});

// 创建 MediaRecorder
mediaRecorder = new MediaRecorder(destination.stream);
mediaRecorder = new MediaRecorder(destination.stream);

mediaRecorder.ondataavailable = event => {
audioChunks.push(event.data);
};

mediaRecorder.onstop = () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
convertToWav(audioBlob).then(wavBlob => {
sendAudioToServer(wavBlob);
});
};
mediaRecorder.ondataavailable = event => {
audioChunks.push(event.data);
};

mediaRecorder.start();
mediaRecorder.onstop = () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
convertToWav(audioBlob).then(wavBlob => {
sendAudioToServer(wavBlob);
});
};

console.log('Recording started, actual sample rate:', audioContext.sampleRate);
mediaRecorder.start();
console.log('Recording started, actual sample rate:', audioContext.sampleRate);
})
.catch(error => {
console.error('Error accessing microphone:', error);
});
}

function stopRecording() {
Expand All @@ -148,16 +140,21 @@ function convertToWav(webmBlob) {

fileReader.onload = function (event) {
const audioData = event.target.result;
console.log('音频数据大小:', audioData.byteLength, '字节');

audioContext.decodeAudioData(audioData, function (buffer) {
console.log('音频解码成功,采样率:', buffer.sampleRate);
const wavBlob = audioBufferToWav(buffer);
resolve(wavBlob);
}, function (e) {
reject('音频解码失败');
console.error('音频解码失败:', e);
// 如果解码失败,我们可以尝试直接发送原始的 WebM 数据
resolve(new Blob([audioData], { type: 'audio/webm' }));
});
};

fileReader.onerror = function (error) {
console.error('文件读取失败:', error);
reject('文件读取失败');
};

Expand Down Expand Up @@ -262,8 +259,12 @@ function writeFloat32(output, offset, input) {
function sendAudioToServer(audioBlob) {
const formData = new FormData();
var timestamp = Date.parse(new Date());
formData.append('audio', audioBlob, timestamp + '.wav');
const fileName = audioBlob.type === 'audio/wav' ? `${timestamp}.wav` : `${timestamp}.webm`;
formData.append('audio', audioBlob, fileName);

console.log('发送到服务器的文件类型:', audioBlob.type);
console.log('文件大小:', audioBlob.size, '字节');

fetch('https://app102.acapp.acwing.com.cn/api/uploadAudio', {
method: 'POST',
mode: 'cors',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Object doAroundService(ProceedingJoinPoint joinPoint) throws Throwable {
if(ipAddress == null){
userMapper.insert(user);
List<Music> musics = musicMapper.selectList(new QueryWrapper<Music>()
.like("url", "https://app102.acapp.acwing.com.cn").last("limit 10").orderByDesc("id"));
.like("url", "https://app102.acapp.acwing.com.cn").last("limit 20").orderByDesc("id"));
user.setMusics(musics);
userMapper.addDefaultMusics(user, new Date());
UserContext.setCurrentUser(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Object recongnizeMusic(@RequestParam("audio") MultipartFile file) {
File destFile = new File(dir.getAbsolutePath() + File.separator + fileName);
file.transferTo(destFile);
Map<String, String> resultMap = acrCloudUtil.recongizeByFile(dir.getAbsolutePath() + File.separator + fileName);
musicMapper.insert(new Music().setArtist(resultMap.get("artist")).setTitle(resultMap.get("title")));
if(resultMap.containsKey("artist") && resultMap.containsKey("title")) musicMapper.insert(new Music().setArtist(resultMap.get("artist")).setTitle(resultMap.get("title")));
return resultMap;
// return MusicRecUtil.recongnizeFile(dir.getAbsolutePath() + File.separator + fileName);

Expand Down
10 changes: 5 additions & 5 deletions musicBackend/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ mybatis-plus:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mybatis/*.xml
#
#file:
# upload:
# path: /root/nginx/share/nginx/media/
# url: https://app102.acapp.acwing.com.cn/media/
file:
upload:
path: D://audio//
path: /root/nginx/share/nginx/media/
url: https://app102.acapp.acwing.com.cn/media/
#file:
# upload:
# path: D://audio//
# url: https://app102.acapp.acwing.com.cn/media/

logging:
level:
Expand Down

0 comments on commit ee333d6

Please sign in to comment.