Skip to content

Commit cc0b347

Browse files
committed
[FEAT] soundeffect 생성 로직 구현
1 parent 367b2e9 commit cc0b347

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

src/main/java/capstone/ses/controller/SoundEffectController.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ public Result createTagRel(@RequestBody List<TagRelDto> tagsRelDto) {
229229
public Result createSoundEffect(@RequestBody SoundEffectRequest soundEffectRequest) {
230230
try {
231231
return new Result(ResultCode.SUCCESS, soundEffectService.createSoundEffect(soundEffectRequest));
232-
} catch (IllegalStateException e) {
232+
} catch (JsonProcessingException e) {
233+
return new Result(ResultCode.FAIL, e.getMessage(), "501");
234+
}
235+
catch (IllegalStateException e) {
233236
return new Result(ResultCode.FAIL, e.getMessage(), "500");
234237
}
235238
}

src/main/java/capstone/ses/service/SoundEffectService.java

+52-29
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.io.IOException;
2929
import java.math.BigDecimal;
3030
import java.util.ArrayList;
31-
import java.util.Base64;
3231
import java.util.List;
3332
import java.util.Map;
3433

@@ -511,38 +510,62 @@ public Boolean updateUnlikedSoundEffect(Long soundEffectId, String accessToken)
511510
}
512511

513512
@Transactional
514-
public byte[] createSoundEffect(SoundEffectRequest soundEffectRequest) {
513+
public SoundEffectCreateDto createSoundEffect(SoundEffectRequest soundEffectRequest) throws JsonProcessingException {
514+
// 헤더 설정
515515
HttpHeaders headers = new HttpHeaders();
516516
headers.setContentType(MediaType.APPLICATION_JSON);
517517

518-
HttpEntity<SoundEffectRequest> requestEntity = new HttpEntity<>(soundEffectRequest, headers);
519-
String base64Data = restTemplate.postForObject("https://aulo-audiogen-956521670074.asia-northeast3.run.app/generate", requestEntity, String.class);
518+
// 요청 데이터 직렬화
519+
ObjectMapper objectMapper = new ObjectMapper();
520+
String jsonRequest = objectMapper.writeValueAsString(soundEffectRequest);
521+
522+
HttpEntity<String> requestEntity = new HttpEntity<>(jsonRequest, headers);
523+
524+
// RestTemplate 호출
525+
ResponseEntity<String> response = restTemplate.postForEntity(
526+
"http://localhost:8001/generate",
527+
requestEntity,
528+
String.class
529+
);
530+
531+
if (!response.getStatusCode().is2xxSuccessful()) {
532+
throw new IllegalStateException("Failed to fetch data from /generate");
533+
}
534+
535+
// JSON 파싱
536+
String responseBody = response.getBody();
537+
List<Map<String, Object>> results = objectMapper.readValue(responseBody, new TypeReference<>() {
538+
});
539+
540+
// 첫 번째 result 가져오기
541+
Map<String, Object> result = results.get(0);
520542

521-
return Base64.getDecoder().decode(base64Data);
543+
return SoundEffectCreateDto.builder()
544+
.file((String) results.get(0).get("base64"))
545+
.soundEffectName((String) results.get(0).get("name"))
546+
.soundEffectTypes(
547+
SoundEffectTypeDto.builder()
548+
.length(convertToSeconds((String) result.get("length")))
549+
.sampleRate(new BigDecimal(result.get("sample_rate").toString()))
550+
.bitDepth((Integer) result.get("bit_depth"))
551+
.channels((String) result.get("channels"))
552+
.fileSize(new BigDecimal(result.get("file_size").toString()))
553+
.build()
554+
)
555+
.build();
522556
}
523557

524-
// private File convertToWav(MultipartFile file) throws IOException, UnsupportedAudioFileException {
525-
// File tempFile = File.createTempFile("upload", ".tmp");
526-
// file.transferTo(tempFile);
527-
//
528-
// File wavFile = new File(tempFile.getParent(), FilenameUtils.getBaseName(file.getOriginalFilename()) + ".wav");
529-
//
530-
// AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(tempFile);
531-
// AudioFormat baseFormat = audioInputStream.getFormat();
532-
// AudioFormat targetFormat = new AudioFormat(
533-
// AudioFormat.Encoding.PCM_SIGNED,
534-
// baseFormat.getSampleRate(),
535-
// 16,
536-
// baseFormat.getChannels(),
537-
// baseFormat.getChannels() * 2,
538-
// baseFormat.getSampleRate(),
539-
// false
540-
// );
541-
//
542-
// AudioInputStream convertedAudioInputStream = AudioSystem.getAudioInputStream(targetFormat, audioInputStream);
543-
// AudioSystem.write(convertedAudioInputStream, AudioFileFormat.Type.WAVE, wavFile);
544-
//
545-
// tempFile.delete();
546-
// return wavFile;
547-
// }
558+
559+
private static int convertToSeconds(String timeString) {
560+
// 시간 형식이 "0:00:05"와 같은 형태라고 가정
561+
String[] parts = timeString.split(":");
562+
563+
// 시, 분, 초를 분리
564+
int hours = Integer.parseInt(parts[0]);
565+
int minutes = Integer.parseInt(parts[1]);
566+
int seconds = Integer.parseInt(parts[2]);
567+
568+
// 초로 변환
569+
return hours * 3600 + minutes * 60 + seconds;
570+
}
548571
}

0 commit comments

Comments
 (0)