|
28 | 28 | import java.io.IOException;
|
29 | 29 | import java.math.BigDecimal;
|
30 | 30 | import java.util.ArrayList;
|
31 |
| -import java.util.Base64; |
32 | 31 | import java.util.List;
|
33 | 32 | import java.util.Map;
|
34 | 33 |
|
@@ -511,38 +510,62 @@ public Boolean updateUnlikedSoundEffect(Long soundEffectId, String accessToken)
|
511 | 510 | }
|
512 | 511 |
|
513 | 512 | @Transactional
|
514 |
| - public byte[] createSoundEffect(SoundEffectRequest soundEffectRequest) { |
| 513 | + public SoundEffectCreateDto createSoundEffect(SoundEffectRequest soundEffectRequest) throws JsonProcessingException { |
| 514 | + // 헤더 설정 |
515 | 515 | HttpHeaders headers = new HttpHeaders();
|
516 | 516 | headers.setContentType(MediaType.APPLICATION_JSON);
|
517 | 517 |
|
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); |
520 | 542 |
|
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(); |
522 | 556 | }
|
523 | 557 |
|
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 | + } |
548 | 571 | }
|
0 commit comments