-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModelService.java
More file actions
167 lines (139 loc) · 6 KB
/
ModelService.java
File metadata and controls
167 lines (139 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package umc.codeplay.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import io.awspring.cloud.sqs.operations.SqsTemplate;
import umc.codeplay.apiPayLoad.code.status.ErrorStatus;
import umc.codeplay.apiPayLoad.exception.GeneralException;
import umc.codeplay.domain.*;
import umc.codeplay.domain.enums.JobType;
import umc.codeplay.domain.enums.ProcessStatus;
import umc.codeplay.dto.MemberRequestDTO;
import umc.codeplay.dto.ModelRequestDTO;
import umc.codeplay.dto.SQSMessageDTO;
import umc.codeplay.repository.HarmonyRepository;
import umc.codeplay.repository.RemixRepository;
import umc.codeplay.repository.TrackRepository;
@Service
@RequiredArgsConstructor
public class ModelService {
private final SqsTemplate sqsTemplate;
private final HarmonyRepository harmonyRepository;
private final TrackRepository trackRepository;
private final RemixRepository remixRepository;
private final TaskService taskService;
@Value("${sqs.queue.name}")
private String queueName;
public Long setHarmony(ModelRequestDTO.HarmonyRequestDTO harmonyResult) {
Task task =
taskService.updateTaskStatus(harmonyResult.getTaskId(), ProcessStatus.COMPLETED);
Harmony harmony = harmonyRepository.findByIdOrThrow(task.getJobId());
harmony.updateHarmonyResult(
harmonyResult.getScale(),
harmonyResult.getBpm(),
harmonyResult.getGenre(),
harmonyResult.getVoiceColor());
return harmonyRepository.save(harmony).getId();
}
public Long setTrack(ModelRequestDTO.TrackRequestDTO trackResult) {
Task task = taskService.updateTaskStatus(trackResult.getTaskId(), ProcessStatus.COMPLETED);
Track track = trackRepository.findByIdOrThrow(task.getJobId());
track.updateTrackResult(
trackResult.getVocalUrl(),
trackResult.getInstrumentalUrl(),
trackResult.getBassUrl(),
trackResult.getDrumsUrl());
return trackRepository.save(track).getId();
}
public Long setRemix(ModelRequestDTO.RemixRequestDTO remixResult) {
Task task = taskService.updateTaskStatus(remixResult.getTaskId(), ProcessStatus.COMPLETED);
Remix remix = remixRepository.findByIdOrThrow(task.getJobId());
remix.setResultMusicUrl(remixResult.getResultMusicUrl());
return remixRepository.save(remix).getId();
}
@Transactional
public Task sendTrackMessage(Music music, String config) {
Task task = taskService.addTask(music, JobType.TRACK);
if (config == null) {
config = "none";
}
switch (config) {
case "vocals", "bass", "drums", "none", "guitar", "piano":
break;
default:
throw new GeneralException(ErrorStatus.INVALID_CONFIG);
}
try {
sqsTemplate.send(
queueName,
SQSMessageDTO.TrackMessageDTO.builder()
.key(music.getTitle())
.musicId(music.getId())
.taskId(task.getId())
.jobType(JobType.TRACK.toString())
.twoStemConfig(config)
.build());
} catch (Exception e) {
throw new GeneralException(ErrorStatus.SQS_SEND_ERROR);
}
return task;
}
@Transactional
public Task sendHarmonyMessage(Music music) {
Task task = taskService.addTask(music, JobType.HARMONY);
try {
sqsTemplate.send(
queueName,
SQSMessageDTO.HarmonyMessageDTO.builder()
.key(music.getTitle())
.musicId(music.getId())
.taskId(task.getId())
.jobType(JobType.HARMONY.toString())
.build());
} catch (Exception e) {
throw new GeneralException(ErrorStatus.SQS_SEND_ERROR);
}
return task;
}
@Transactional
public Task sendRemixMessage(Music music, MemberRequestDTO.RemixTaskDTO request) {
Remix parentRemix = null;
SQSMessageDTO.RemixMessageDTO remixPayLoad =
SQSMessageDTO.RemixMessageDTO.builder().build();
if (request.getParentRemixId() != null) {
parentRemix = remixRepository.findByIdOrThrow(request.getParentRemixId());
if (!parentRemix.getMusic().getId().equals(music.getId())) {
throw new GeneralException(ErrorStatus.INVALID_PARENT_REMIX);
}
remixPayLoad.setRemix(parentRemix, request);
} else {
remixPayLoad.setRemix(request);
}
Remix newRemix =
remixRepository.save(
Remix.builder()
.music(music)
.scaleModulation(remixPayLoad.getScaleModulation())
.tempoRatio(remixPayLoad.getTempoRatio())
.reverbAmount(remixPayLoad.getReverbAmount())
.isChorusOn(remixPayLoad.getIsChorusOn())
.build());
if (parentRemix != null) {
newRemix.setParentRemix(parentRemix);
parentRemix.getChildRemixList().add(newRemix);
remixRepository.save(parentRemix);
}
Task task = taskService.addTask(newRemix);
remixPayLoad.setKey(music.getTitle());
remixPayLoad.setMusicId(music.getId());
remixPayLoad.setTaskId(task.getId());
remixPayLoad.setJobType(JobType.REMIX.toString());
try {
sqsTemplate.send(queueName, remixPayLoad);
} catch (Exception e) {
throw new GeneralException(ErrorStatus.SQS_SEND_ERROR);
}
return task;
}
}