Merge feature/stt-ai into main

주요 변경사항:
- EventHub 공유 액세스 정책 재설정 (send-policy, listen-policy)
- Redis DB 2번 읽기 전용 문제 해결
- AI-Python 서비스 추가 (FastAPI 기반)
- STT WebSocket 실시간 스트리밍 구현
- AI 제안사항 실시간 추출 기능 구현
- 테스트 페이지 추가 (stt-test-wav.html)
- 개발 가이드 문서 추가

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Minseo-Jo
2025-10-29 16:01:47 +09:00
81 changed files with 10269 additions and 276 deletions
@@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* STT Service Application
@@ -21,6 +22,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
"com.unicorn.hgzero.stt.repository.jpa",
"com.unicorn.hgzero.common.repository"
})
@EnableScheduling // 배치 작업 스케줄링 활성화
public class SttApplication {
public static void main(String[] args) {
@@ -0,0 +1,65 @@
package com.unicorn.hgzero.stt.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis Stream 설정
* 오디오 데이터 버퍼링용
*/
@Configuration
public class RedisStreamConfig {
/**
* 오디오 데이터 저장용 RedisTemplate
*/
@Bean(name = "audioRedisTemplate")
public RedisTemplate<String, byte[]> audioRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// Key Serializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
// Value Serializer (byte array는 기본 직렬화 사용)
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
/**
* 일반 데이터 저장용 StringRedisTemplate
*/
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
/**
* 범용 Object 저장용 RedisTemplate
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// Key Serializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
// Value Serializer
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
@@ -1,8 +1,5 @@
package com.unicorn.hgzero.stt.config;
import com.unicorn.hgzero.common.security.JwtTokenProvider;
import com.unicorn.hgzero.common.security.filter.JwtAuthenticationFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -11,7 +8,6 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
@@ -20,15 +16,12 @@ import java.util.Arrays;
/**
* Spring Security 설정
* JWT 기반 인증 및 API 보안 설정
* CORS 설정 및 API 보안 설정 (인증 없음)
*/
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtTokenProvider jwtTokenProvider;
@Value("${cors.allowed-origins:http://localhost:3000,http://localhost:8080,http://localhost:8081,http://localhost:8082,http://localhost:8083,http://localhost:8084}")
private String allowedOrigins;
@@ -39,19 +32,9 @@ public class SecurityConfig {
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
// Actuator endpoints
.requestMatchers("/actuator/**").permitAll()
// Swagger UI endpoints - context path와 상관없이 접근 가능하도록 설정
.requestMatchers("/swagger-ui/**", "/swagger-ui.html", "/v3/api-docs/**", "/swagger-resources/**", "/webjars/**").permitAll()
// Health check
.requestMatchers("/health").permitAll()
// WebSocket endpoints
.requestMatchers("/ws/**").permitAll()
// All other requests require authentication
.anyRequest().authenticated()
// 모든 요청 허용 (인증 없음)
.anyRequest().permitAll()
)
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider),
UsernamePasswordAuthenticationFilter.class)
.build();
}
@@ -1,9 +1,13 @@
package com.unicorn.hgzero.stt.config;
import com.unicorn.hgzero.stt.controller.AudioWebSocketHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
/**
* WebSocket 설정
@@ -11,51 +15,27 @@ import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry
*/
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {
private final AudioWebSocketHandler audioWebSocketHandler;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
// 실시간 STT WebSocket 엔드포인트 등록
registry.addHandler(new SttWebSocketHandler(), "/ws/stt/{sessionId}")
// 오디오 스트리밍 WebSocket 엔드포인트
registry.addHandler(audioWebSocketHandler, "/ws/audio")
.setAllowedOrigins("*"); // 실제 운영 환경에서는 특정 도메인으로 제한
}
/**
* STT WebSocket 핸들러
* 실시간 음성 데이터 수신 및 처리
* WebSocket 메시지 버퍼 크기 설정
* 오디오 청크 전송을 위해 충분한 버퍼 크기 확보 (10MB)
*/
private static class SttWebSocketHandler implements org.springframework.web.socket.WebSocketHandler {
@Override
public void afterConnectionEstablished(org.springframework.web.socket.WebSocketSession session) throws Exception {
System.out.println("STT WebSocket 연결 설정: " + session.getId());
// 실제로는 Azure Speech Service 스트리밍 연결 설정
}
@Override
public void handleMessage(org.springframework.web.socket.WebSocketSession session,
org.springframework.web.socket.WebSocketMessage<?> message) throws Exception {
// 실시간 음성 데이터 처리
System.out.println("음성 데이터 수신: " + message.getPayload());
// 실제로는 TranscriptionService.processAudioStream() 호출
}
@Override
public void handleTransportError(org.springframework.web.socket.WebSocketSession session,
Throwable exception) throws Exception {
System.err.println("WebSocket 전송 오류: " + exception.getMessage());
}
@Override
public void afterConnectionClosed(org.springframework.web.socket.WebSocketSession session,
org.springframework.web.socket.CloseStatus closeStatus) throws Exception {
System.out.println("STT WebSocket 연결 종료: " + session.getId());
// 리소스 정리
}
@Override
public boolean supportsPartialMessages() {
return false;
}
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(10 * 1024 * 1024); // 10MB
container.setMaxBinaryMessageBufferSize(10 * 1024 * 1024); // 10MB
return container;
}
}
@@ -0,0 +1,225 @@
package com.unicorn.hgzero.stt.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.unicorn.hgzero.stt.dto.AudioChunkDto;
import com.unicorn.hgzero.stt.service.AudioBufferService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 오디오 WebSocket 핸들러
* 프론트엔드에서 실시간 오디오 스트림을 수신하고 STT 결과를 전송
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class AudioWebSocketHandler extends AbstractWebSocketHandler {
private final AudioBufferService audioBufferService;
private final ObjectMapper objectMapper;
// 세션별 회의 ID 매핑
private final Map<String, String> sessionMeetingMap = new ConcurrentHashMap<>();
// 회의 ID별 세션 목록 (결과 브로드캐스트용)
private final Map<String, Set<WebSocketSession>> meetingSessionsMap = new ConcurrentHashMap<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
log.info("WebSocket 연결 성공 - sessionId: {}", session.getId());
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
try {
String payload = message.getPayload();
// JSON 파싱
Map<String, Object> data = objectMapper.readValue(payload, Map.class);
String type = (String) data.get("type");
if ("start".equals(type)) {
// 녹음 시작
String meetingId = (String) data.get("meetingId");
sessionMeetingMap.put(session.getId(), meetingId);
// 세션을 회의별 목록에 추가
meetingSessionsMap.computeIfAbsent(meetingId, k -> ConcurrentHashMap.newKeySet())
.add(session);
log.info("녹음 시작 - sessionId: {}, meetingId: {}", session.getId(), meetingId);
// 응답 전송
session.sendMessage(new TextMessage("{\"status\":\"started\",\"meetingId\":\"" + meetingId + "\"}"));
} else if ("chunk".equals(type)) {
// 오디오 청크 수신 (Base64 인코딩)
handleAudioChunk(session, data);
} else if ("stop".equals(type)) {
// 녹음 종료
String meetingId = sessionMeetingMap.get(session.getId());
log.info("녹음 종료 - sessionId: {}, meetingId: {}", session.getId(), meetingId);
// 응답 전송
session.sendMessage(new TextMessage("{\"status\":\"stopped\"}"));
}
} catch (Exception e) {
log.error("텍스트 메시지 처리 실패 - sessionId: {}", session.getId(), e);
session.sendMessage(new TextMessage("{\"error\":\"" + e.getMessage() + "\"}"));
}
}
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
try {
String meetingId = sessionMeetingMap.get(session.getId());
if (meetingId == null) {
log.warn("회의 ID 없음 - sessionId: {}", session.getId());
return;
}
byte[] audioData = message.getPayload().array();
// 오디오 청크 생성
AudioChunkDto chunk = AudioChunkDto.builder()
.meetingId(meetingId)
.audioData(audioData)
.timestamp(System.currentTimeMillis())
.chunkIndex(0) // 바이너리 메시지는 인덱스 없음
.format("audio/webm")
.sampleRate(16000)
.build();
// Redis에 버퍼링
audioBufferService.bufferAudioChunk(chunk);
log.debug("오디오 바이너리 수신 - sessionId: {}, size: {} bytes",
session.getId(), audioData.length);
} catch (Exception e) {
log.error("바이너리 메시지 처리 실패 - sessionId: {}", session.getId(), e);
}
}
/**
* JSON으로 전송된 오디오 청크 처리
*/
private void handleAudioChunk(WebSocketSession session, Map<String, Object> data) {
try {
String meetingId = (String) data.get("meetingId");
String audioBase64 = (String) data.get("audioData");
Long timestamp = data.get("timestamp") != null
? ((Number) data.get("timestamp")).longValue()
: System.currentTimeMillis();
Integer chunkIndex = data.get("chunkIndex") != null
? ((Number) data.get("chunkIndex")).intValue()
: 0;
// Base64 디코딩
byte[] audioData = Base64.getDecoder().decode(audioBase64);
// 오디오 청크 생성
AudioChunkDto chunk = AudioChunkDto.builder()
.meetingId(meetingId)
.audioData(audioData)
.timestamp(timestamp)
.chunkIndex(chunkIndex)
.format((String) data.getOrDefault("format", "audio/webm"))
.sampleRate(data.get("sampleRate") != null
? ((Number) data.get("sampleRate")).intValue()
: 16000)
.build();
// Redis에 버퍼링
audioBufferService.bufferAudioChunk(chunk);
log.debug("오디오 청크 수신 - meetingId: {}, chunkIndex: {}, size: {} bytes",
meetingId, chunkIndex, audioData.length);
} catch (Exception e) {
log.error("오디오 청크 처리 실패 - sessionId: {}", session.getId(), e);
}
}
/**
* STT 결과를 특정 회의의 모든 클라이언트에게 전송
*/
public void sendTranscriptToMeeting(String meetingId, String text, double confidence) {
Set<WebSocketSession> sessions = meetingSessionsMap.get(meetingId);
if (sessions == null || sessions.isEmpty()) {
log.debug("전송할 세션 없음 - meetingId: {}", meetingId);
return;
}
try {
Map<String, Object> result = new HashMap<>();
result.put("transcript", text);
result.put("confidence", confidence);
result.put("timestamp", System.currentTimeMillis());
result.put("speaker", "참석자");
String jsonMessage = objectMapper.writeValueAsString(result);
TextMessage message = new TextMessage(jsonMessage);
// 모든 세션에 브로드캐스트
Iterator<WebSocketSession> iterator = sessions.iterator();
while (iterator.hasNext()) {
WebSocketSession session = iterator.next();
try {
if (session.isOpen()) {
session.sendMessage(message);
} else {
iterator.remove();
}
} catch (Exception e) {
log.error("메시지 전송 실패 - sessionId: {}", session.getId(), e);
iterator.remove();
}
}
log.info("STT 결과 전송 완료 - meetingId: {}, sessions: {}개, text: {}",
meetingId, sessions.size(), text);
} catch (Exception e) {
log.error("STT 결과 전송 실패 - meetingId: {}", meetingId, e);
}
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
String meetingId = sessionMeetingMap.remove(session.getId());
// 회의별 세션 목록에서도 제거
if (meetingId != null) {
Set<WebSocketSession> sessions = meetingSessionsMap.get(meetingId);
if (sessions != null) {
sessions.remove(session);
if (sessions.isEmpty()) {
meetingSessionsMap.remove(meetingId);
}
}
}
log.info("WebSocket 연결 종료 - sessionId: {}, meetingId: {}, status: {}",
session.getId(), meetingId, status);
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
log.error("WebSocket 전송 오류 - sessionId: {}", session.getId(), exception);
}
}
@@ -0,0 +1,47 @@
package com.unicorn.hgzero.stt.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 오디오 청크 DTO
* 프론트엔드에서 전송되는 오디오 데이터
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AudioChunkDto {
/**
* 회의 ID
*/
private String meetingId;
/**
* 오디오 데이터 (Base64 인코딩 또는 바이트 배열)
*/
private byte[] audioData;
/**
* 타임스탬프 (밀리초)
*/
private Long timestamp;
/**
* 청크 인덱스 (순서 보장)
*/
private Integer chunkIndex;
/**
* 오디오 포맷 (예: "audio/webm", "audio/wav")
*/
private String format;
/**
* 샘플링 레이트 (예: 16000, 44100)
*/
private Integer sampleRate;
}
@@ -1,8 +1,8 @@
package com.unicorn.hgzero.stt.dto;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import lombok.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
@@ -16,26 +16,33 @@ import java.time.LocalDateTime;
* 녹음 관련 DTO 클래스들
*/
public class RecordingDto {
/**
* 녹음 준비 요청 DTO
*/
@Getter
@Builder
@ToString
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonDeserialize(builder = PrepareRequest.PrepareRequestBuilder.class)
public static class PrepareRequest {
@NotBlank(message = "회의 ID는 필수입니다")
private final String meetingId;
@NotBlank(message = "세션 ID는 필수입니다")
private final String sessionId;
private final String language;
@Min(value = 1, message = "참석자 수는 1명 이상이어야 합니다")
@Max(value = 50, message = "참석자 수는 50명을 초과할 수 없습니다")
private final Integer attendeeCount;
@JsonPOJOBuilder(withPrefix = "")
public static class PrepareRequestBuilder {
}
}
/**
@@ -59,12 +66,19 @@ public class RecordingDto {
@Getter
@Builder
@ToString
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonDeserialize(builder = StartRequest.StartRequestBuilder.class)
public static class StartRequest {
@NotBlank(message = "시작자 ID는 필수입니다")
private final String startedBy;
private final String recordingMode;
@JsonPOJOBuilder(withPrefix = "")
public static class StartRequestBuilder {
}
}
/**
@@ -73,12 +87,19 @@ public class RecordingDto {
@Getter
@Builder
@ToString
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonDeserialize(builder = StopRequest.StopRequestBuilder.class)
public static class StopRequest {
@NotBlank(message = "중지자 ID는 필수입니다")
private final String stoppedBy;
private final String reason;
@JsonPOJOBuilder(withPrefix = "")
public static class StopRequestBuilder {
}
}
/**
@@ -0,0 +1,156 @@
package com.unicorn.hgzero.stt.service;
import com.unicorn.hgzero.stt.controller.AudioWebSocketHandler;
import com.unicorn.hgzero.stt.dto.AudioChunkDto;
import com.unicorn.hgzero.stt.event.TranscriptionEvent;
import com.unicorn.hgzero.stt.event.publisher.EventPublisher;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.UUID;
/**
* 오디오 배치 프로세서
* 5초마다 Redis에 축적된 오디오를 처리하여 텍스트로 변환
*
* Note: STT 결과는 DB에 저장하지 않고, Event Hub와 WebSocket으로만 전송
* 최종 회의록은 AI 서비스에서 저장
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class AudioBatchProcessor {
private final AudioBufferService audioBufferService;
private final AzureSpeechService azureSpeechService;
private final EventPublisher eventPublisher;
private final AudioWebSocketHandler webSocketHandler;
/**
* 5초마다 오디오 배치 처리
* - Redis에서 오디오 청크 조회
* - Azure Speech로 텍스트 변환
* - Event Hub 이벤트 발행 (AI 서비스로 전송)
* - WebSocket 실시간 전송 (클라이언트 표시)
*/
@Scheduled(fixedDelay = 5000, initialDelay = 10000) // 5초마다 실행, 최초 10초 후 시작
public void processAudioBatch() {
try {
// 활성 회의 목록 조회
Set<String> activeMeetings = audioBufferService.getActiveMeetings();
if (activeMeetings.isEmpty()) {
log.debug("활성 회의 없음 - 배치 처리 스킵");
return;
}
log.info("배치 처리 시작 - 활성 회의: {}개", activeMeetings.size());
// 각 회의별로 처리
for (String meetingId : activeMeetings) {
processOneMeeting(meetingId);
}
log.info("배치 처리 완료");
} catch (Exception e) {
log.error("배치 처리 실패", e);
}
}
/**
* 하나의 회의에 대한 오디오 처리
*/
private void processOneMeeting(String meetingId) {
try {
// Redis에서 최근 5초 오디오 청크 조회
List<AudioChunkDto> chunks = audioBufferService.getAudioChunks(meetingId);
if (chunks.isEmpty()) {
log.debug("오디오 청크 없음 - meetingId: {}", meetingId);
return;
}
log.info("오디오 청크 조회 완료 - meetingId: {}, chunks: {}개", meetingId, chunks.size());
// 오디오 청크 병합 (5초 분량)
byte[] mergedAudio = audioBufferService.mergeAudioChunks(chunks);
if (mergedAudio.length == 0) {
log.warn("병합된 오디오 없음 - meetingId: {}", meetingId);
return;
}
// Azure Speech API로 음성 인식
AzureSpeechService.RecognitionResult result = azureSpeechService.recognizeAudio(mergedAudio);
if (!result.isSuccess() || result.getText().trim().isEmpty()) {
log.debug("음성 인식 결과 없음 - meetingId: {}", meetingId);
// Redis 청크는 삭제 (무음 또는 인식 불가)
audioBufferService.clearProcessedChunks(meetingId);
return;
}
// Event Hub 이벤트 발행 (AI 서비스로 전송)
publishTranscriptionEvent(meetingId, result);
// WebSocket으로 실시간 결과 전송 (클라이언트 표시)
sendTranscriptToClients(meetingId, result);
// Redis 정리
audioBufferService.clearProcessedChunks(meetingId);
log.info("회의 처리 완료 - meetingId: {}, text: {}", meetingId, result.getText());
} catch (Exception e) {
log.error("회의 처리 실패 - meetingId: {}", meetingId, e);
}
}
/**
* Event Hub 이벤트 발행 (AI 서비스로 전송)
* AI 서비스에서 Claude API로 제안사항 분석 후 처리
*/
private void publishTranscriptionEvent(String meetingId, AzureSpeechService.RecognitionResult result) {
try {
// 간소화된 이벤트 (TranscriptSegmentReady)
TranscriptionEvent.SegmentCreated event = TranscriptionEvent.SegmentCreated.of(
UUID.randomUUID().toString(), // segmentId
meetingId, // recordingId
meetingId, // meetingId
result.getText(), // text
"UNKNOWN", // speakerId
"참석자", // speakerName
LocalDateTime.now(), // timestamp
5.0, // duration
result.getConfidence(), // confidence
result.getConfidence() < 0.6 // warningFlag
);
eventPublisher.publishAsync("transcription-events", event);
log.debug("Event Hub 이벤트 발행 완료 - meetingId: {}, text: {}",
meetingId, result.getText());
} catch (Exception e) {
log.error("Event Hub 이벤트 발행 실패 - meetingId: {}", meetingId, e);
}
}
/**
* WebSocket으로 STT 결과를 클라이언트에게 실시간 전송
*/
private void sendTranscriptToClients(String meetingId, AzureSpeechService.RecognitionResult result) {
try {
webSocketHandler.sendTranscriptToMeeting(meetingId, result.getText(), result.getConfidence());
log.debug("WebSocket 결과 전송 완료 - meetingId: {}, text: {}", meetingId, result.getText());
} catch (Exception e) {
log.error("WebSocket 결과 전송 실패 - meetingId: {}", meetingId, e);
}
}
}
@@ -0,0 +1,211 @@
package com.unicorn.hgzero.stt.service;
import com.unicorn.hgzero.stt.dto.AudioChunkDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* 오디오 버퍼 서비스
* Redis Stream을 사용하여 오디오 청크를 버퍼링
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class AudioBufferService {
private final RedisTemplate<String, Object> redisTemplate;
// Redis 키 패턴
private static final String AUDIO_STREAM_PREFIX = "audio:stream:";
private static final String ACTIVE_MEETINGS_KEY = "active:meetings";
private static final long STREAM_TTL_SECONDS = 60; // 1분 후 자동 삭제
/**
* 오디오 청크를 Redis Stream에 저장
*
* @param chunk 오디오 청크
*/
public void bufferAudioChunk(AudioChunkDto chunk) {
try {
String streamKey = getStreamKey(chunk.getMeetingId());
// 바이트 배열을 Base64로 인코딩
String encodedAudioData = Base64.getEncoder().encodeToString(chunk.getAudioData());
// Hash 형태로 저장
Map<String, Object> data = Map.of(
"audioData", encodedAudioData,
"timestamp", chunk.getTimestamp(),
"chunkIndex", chunk.getChunkIndex(),
"format", chunk.getFormat() != null ? chunk.getFormat() : "audio/webm",
"sampleRate", chunk.getSampleRate() != null ? chunk.getSampleRate() : 16000
);
// Redis Stream에 추가 (XADD)
redisTemplate.opsForStream().add(streamKey, data);
// 활성 회의 목록에 추가
redisTemplate.opsForSet().add(ACTIVE_MEETINGS_KEY, chunk.getMeetingId());
// TTL 설정 (1분)
redisTemplate.expire(streamKey, STREAM_TTL_SECONDS, TimeUnit.SECONDS);
log.debug("오디오 청크 버퍼링 완료 - meetingId: {}, chunkIndex: {}",
chunk.getMeetingId(), chunk.getChunkIndex());
} catch (Exception e) {
log.error("오디오 청크 버퍼링 실패 - meetingId: {}", chunk.getMeetingId(), e);
}
}
/**
* 회의별 활성 오디오 청크 조회
*
* @param meetingId 회의 ID
* @return 오디오 청크 리스트
*/
public List<AudioChunkDto> getAudioChunks(String meetingId) {
try {
String streamKey = getStreamKey(meetingId);
List<AudioChunkDto> chunks = new ArrayList<>();
// Redis Stream에서 모든 데이터 조회 (XRANGE)
List<MapRecord<String, Object, Object>> records = redisTemplate.opsForStream()
.range(streamKey, org.springframework.data.domain.Range.unbounded());
if (records == null || records.isEmpty()) {
return chunks;
}
// MapRecord를 AudioChunkDto로 변환
for (MapRecord<String, Object, Object> record : records) {
Map<Object, Object> value = record.getValue();
// Base64로 인코딩된 문자열을 바이트 배열로 디코딩
String encodedAudioData = (String) value.get("audioData");
byte[] audioData = Base64.getDecoder().decode(encodedAudioData);
AudioChunkDto chunk = AudioChunkDto.builder()
.meetingId(meetingId)
.audioData(audioData)
.timestamp(Long.valueOf(value.get("timestamp").toString()))
.chunkIndex(Integer.valueOf(value.get("chunkIndex").toString()))
.format((String) value.get("format"))
.sampleRate(Integer.valueOf(value.get("sampleRate").toString()))
.build();
chunks.add(chunk);
}
log.debug("오디오 청크 조회 완료 - meetingId: {}, count: {}", meetingId, chunks.size());
return chunks;
} catch (Exception e) {
log.error("오디오 청크 조회 실패 - meetingId: {}", meetingId, e);
return new ArrayList<>();
}
}
/**
* 처리된 오디오 청크 삭제
*
* @param meetingId 회의 ID
*/
public void clearProcessedChunks(String meetingId) {
try {
String streamKey = getStreamKey(meetingId);
// 스트림 전체 삭제
redisTemplate.delete(streamKey);
log.debug("오디오 청크 삭제 완료 - meetingId: {}", meetingId);
} catch (Exception e) {
log.error("오디오 청크 삭제 실패 - meetingId: {}", meetingId, e);
}
}
/**
* 활성 회의 목록 조회
*
* @return 활성 회의 ID 목록
*/
public Set<String> getActiveMeetings() {
try {
Set<Object> meetings = redisTemplate.opsForSet().members(ACTIVE_MEETINGS_KEY);
if (meetings == null) {
return Set.of();
}
return meetings.stream()
.map(Object::toString)
.collect(java.util.stream.Collectors.toSet());
} catch (Exception e) {
log.error("활성 회의 목록 조회 실패", e);
return Set.of();
}
}
/**
* 회의를 활성 목록에서 제거
*
* @param meetingId 회의 ID
*/
public void removeMeetingFromActive(String meetingId) {
try {
redisTemplate.opsForSet().remove(ACTIVE_MEETINGS_KEY, meetingId);
log.info("회의 비활성화 - meetingId: {}", meetingId);
} catch (Exception e) {
log.error("회의 비활성화 실패 - meetingId: {}", meetingId, e);
}
}
/**
* 오디오 청크 병합
*
* @param chunks 오디오 청크 리스트
* @return 병합된 오디오 데이터
*/
public byte[] mergeAudioChunks(List<AudioChunkDto> chunks) {
if (chunks == null || chunks.isEmpty()) {
return new byte[0];
}
// 청크 인덱스 순서로 정렬
chunks.sort((a, b) -> Integer.compare(a.getChunkIndex(), b.getChunkIndex()));
// 전체 크기 계산
int totalSize = chunks.stream()
.mapToInt(chunk -> chunk.getAudioData().length)
.sum();
// 병합
byte[] mergedAudio = new byte[totalSize];
int offset = 0;
for (AudioChunkDto chunk : chunks) {
System.arraycopy(chunk.getAudioData(), 0, mergedAudio, offset, chunk.getAudioData().length);
offset += chunk.getAudioData().length;
}
log.debug("오디오 청크 병합 완료 - chunks: {}, totalSize: {}", chunks.size(), totalSize);
return mergedAudio;
}
/**
* Redis Stream 키 생성
*/
private String getStreamKey(String meetingId) {
return AUDIO_STREAM_PREFIX + meetingId;
}
}
@@ -0,0 +1,208 @@
package com.unicorn.hgzero.stt.service;
import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
import com.microsoft.cognitiveservices.speech.audio.AudioInputStream;
import com.microsoft.cognitiveservices.speech.audio.PushAudioInputStream;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* Azure Speech Service 연동 서비스
* 배치 처리용 음성 인식 기능 제공
*/
@Slf4j
@Service
public class AzureSpeechService {
@Value("${azure.speech.subscription-key}")
private String subscriptionKey;
@Value("${azure.speech.region}")
private String region;
@Value("${azure.speech.language:ko-KR}")
private String language;
private SpeechConfig speechConfig;
@PostConstruct
public void init() {
try {
log.info("Azure Speech Service 초기화 시작 - subscriptionKey: {}, region: {}",
subscriptionKey != null && !subscriptionKey.trim().isEmpty() ? "설정됨" : "미설정", region);
if (subscriptionKey == null || subscriptionKey.trim().isEmpty()) {
log.warn("Azure Speech Subscription Key 미설정 - 시뮬레이션 모드로 실행");
return;
}
speechConfig = SpeechConfig.fromSubscription(subscriptionKey, region);
speechConfig.setSpeechRecognitionLanguage(language);
// 연속 인식 설정 최적화
speechConfig.setProperty(PropertyId.SpeechServiceConnection_EndSilenceTimeoutMs, "3000");
speechConfig.setProperty(PropertyId.SpeechServiceConnection_InitialSilenceTimeoutMs, "10000");
log.info("Azure Speech Service 초기화 완료 - Region: {}, Language: {}", region, language);
} catch (Exception e) {
log.error("Azure Speech Service 초기화 실패", e);
throw new RuntimeException("Azure Speech Service 초기화 실패", e);
}
}
/**
* 오디오 데이터를 텍스트로 변환 (배치 처리용)
*
* @param audioData 병합된 오디오 데이터 (5초 분량)
* @return 인식 결과
*/
public RecognitionResult recognizeAudio(byte[] audioData) {
if (!isAvailable()) {
log.warn("Azure Speech Service 비활성화 - 시뮬레이션 결과 반환");
return createSimulationResult();
}
PushAudioInputStream pushStream = null;
SpeechRecognizer recognizer = null;
try {
// Push 오디오 스트림 생성
pushStream = AudioInputStream.createPushStream();
AudioConfig audioConfig = AudioConfig.fromStreamInput(pushStream);
// 음성 인식기 생성
recognizer = new SpeechRecognizer(speechConfig, audioConfig);
// 오디오 데이터 전송
pushStream.write(audioData);
pushStream.close();
// 인식 실행 (동기 방식)
SpeechRecognitionResult result = recognizer.recognizeOnceAsync().get();
// 결과 처리
return processRecognitionResult(result);
} catch (Exception e) {
log.error("음성 인식 실패", e);
return new RecognitionResult("", 0.0, false);
} finally {
// 리소스 정리
if (recognizer != null) {
recognizer.close();
}
}
}
/**
* Azure Speech 인식 결과 처리
*/
private RecognitionResult processRecognitionResult(SpeechRecognitionResult result) {
if (result.getReason() == ResultReason.RecognizedSpeech) {
String text = result.getText();
double confidence = calculateConfidence(text);
log.info("음성 인식 성공: {}, 신뢰도: {:.2f}", text, confidence);
return new RecognitionResult(text, confidence, true);
} else if (result.getReason() == ResultReason.NoMatch) {
log.debug("음성 인식 실패 - NoMatch (무음 또는 인식 불가)");
return new RecognitionResult("", 0.0, false);
} else if (result.getReason() == ResultReason.Canceled) {
CancellationDetails cancellation = CancellationDetails.fromResult(result);
log.error("음성 인식 취소 - Reason: {}, Details: {}",
cancellation.getReason(), cancellation.getErrorDetails());
return new RecognitionResult("", 0.0, false);
}
return new RecognitionResult("", 0.0, false);
}
/**
* 신뢰도 계산 (추정)
* Azure Speech는 confidence를 직접 제공하지 않으므로 텍스트 길이 기반 추정
*/
private double calculateConfidence(String text) {
if (text == null || text.trim().isEmpty()) {
return 0.0;
}
// 텍스트 길이 기반 휴리스틱
int length = text.length();
if (length > 50) return 0.95;
if (length > 20) return 0.85;
if (length > 10) return 0.75;
return 0.65;
}
/**
* 시뮬레이션 결과 생성 (Azure Speech 비활성화 시)
*/
private RecognitionResult createSimulationResult() {
String[] sampleTexts = {
"신제품 개발 일정에 대해 논의하고 있습니다.",
"마케팅 예산 배분 계획을 수립해야 합니다.",
"다음 주까지 프로토타입을 완성하기로 했습니다.",
"고객 피드백을 반영한 개선안을 검토 중입니다.",
"프로젝트 일정 조정이 필요할 것 같습니다.",
"기술 스택 선정에 대한 의견을 나누고 있습니다."
};
int index = (int) (Math.random() * sampleTexts.length);
String text = sampleTexts[index];
log.info("[시뮬레이션] 음성 인식 결과: {}", text);
return new RecognitionResult(text, 0.85, true);
}
/**
* Azure Speech Service 사용 가능 여부
*/
public boolean isAvailable() {
return speechConfig != null &&
subscriptionKey != null &&
!subscriptionKey.trim().isEmpty();
}
@PreDestroy
public void cleanup() {
if (speechConfig != null) {
speechConfig.close();
log.info("Azure Speech Service 종료");
}
}
/**
* 인식 결과 DTO
*/
public static class RecognitionResult {
private final String text;
private final double confidence;
private final boolean success;
public RecognitionResult(String text, double confidence, boolean success) {
this.text = text;
this.confidence = confidence;
this.success = success;
}
public String getText() {
return text;
}
public double getConfidence() {
return confidence;
}
public boolean isSuccess() {
return success;
}
}
}