Repository navigation
[REFACTOR] MIDI 이벤트 저장 방식 수정 #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
36cf507
af1a11b
8b3808b
119a6df
fdddd45
9ae4ccd
c9ab86f
126ebe5
af997bd
24b79ed
615fc67
5d73598
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,67 +3,43 @@ | |
| import com.mr.domain.playing.entity.enums.MidiType; | ||
| import com.mr.domain.playing.exception.PlayingErrorStatus; | ||
| import com.mr.global.apipayload.exception.GeneralException; | ||
| import jakarta.persistence.*; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Table( | ||
| name = "midi_event", | ||
| indexes = { | ||
| @Index(name = "idx_midi_event_playing_timestamp", | ||
| columnList = "playing_id, timestamp_ms") | ||
| } | ||
| ) | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class MidiEvent { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "midi_event_id") | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "playing_id", nullable = false) | ||
| private Playing playing; | ||
| public class MidiEventData { | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(name = "type", nullable = false) | ||
| private MidiType type; | ||
|
|
||
| @Column(name = "pitch", nullable = false) | ||
| private Integer pitch; | ||
|
|
||
| @Column(name = "velocity", nullable = false) | ||
| private Integer velocity; | ||
|
|
||
| @Column(name = "timestamp_ms", nullable = false) | ||
| private Long timestampMs; | ||
|
|
||
| @Builder(access = AccessLevel.PRIVATE) | ||
| private MidiEvent( | ||
| Playing playing, MidiType type, Integer pitch, Integer velocity, Long timestampMs | ||
| private MidiEventData( | ||
| MidiType type, | ||
| Integer pitch, | ||
| Integer velocity, | ||
| Long timestampMs | ||
| ) { | ||
| validatePlaying(playing); | ||
| validateMidiType(type); | ||
| validatePitch(pitch); | ||
| validateVelocity(velocity); | ||
|
Comment on lines
+33
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MidiType을 지금 NOTE_ON, NOTE_OFF만 갖고 있는데 저희 MIDI 데이터를 요 정도만 받아도 되는 건지 궁금해요! (이건 PM님과 논의할 사안인 것 같지만....) 현재는 모든 타입에 pitch와 velocity를 필수로 검증하고 있어, 추후 페달이나 Control Change 이벤트까지 저장한다면 타입별 검증 또는 이벤트 모델 분리가 필요할 것 같아요!! |
||
| validateTimestampMs(timestampMs); | ||
|
|
||
| this.playing = playing; | ||
| this.type = type; | ||
| this.pitch = pitch; | ||
| this.velocity = velocity; | ||
| this.timestampMs = timestampMs; | ||
| } | ||
|
|
||
| private static void validatePlaying(Playing playing) { | ||
| if (playing == null) { | ||
| throw new GeneralException(PlayingErrorStatus.MISSING_PLAYING); | ||
| } | ||
| public static MidiEventData create( | ||
| MidiType type, | ||
| Integer pitch, | ||
| Integer velocity, | ||
| Long timestampMs | ||
| ) { | ||
| return new MidiEventData(type, pitch, velocity, timestampMs); | ||
| } | ||
|
ownue marked this conversation as resolved.
Resolved
|
||
|
|
||
| private static void validateMidiType(MidiType type) { | ||
|
|
@@ -89,16 +65,4 @@ private static void validateTimestampMs(Long timestampMs) { | |
| throw new GeneralException(PlayingErrorStatus.INVALID_TIMESTAMP); | ||
| } | ||
| } | ||
|
|
||
| public static MidiEvent create( | ||
| Playing playing, MidiType type, Integer pitch, Integer velocity, Long timestampMs | ||
| ) { | ||
| return MidiEvent.builder() | ||
| .playing(playing) | ||
| .type(type) | ||
| .pitch(pitch) | ||
| .velocity(velocity) | ||
| .timestampMs(timestampMs) | ||
| .build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,26 @@ | |
| import com.mr.domain.playing.exception.PlayingErrorStatus; | ||
| import com.mr.global.apipayload.exception.GeneralException; | ||
| import com.mr.global.entity.BaseCreatedDeletedEntity; | ||
| import jakarta.persistence.*; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.hibernate.annotations.JdbcTypeCode; | ||
| import org.hibernate.type.SqlTypes; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| @Entity | ||
| @Table(name = "playing") | ||
|
|
@@ -47,9 +60,6 @@ public class Playing extends BaseCreatedDeletedEntity { | |
| @Column(name = "bpm", nullable = false) | ||
| private Integer bpm; | ||
|
|
||
| @Column(name = "metronome_enabled", nullable = false) | ||
| private boolean metronomeEnabled; | ||
|
|
||
| // 실제 연주가 시작된 시간 | ||
| @Column(name = "started_at") | ||
| private LocalDateTime startedAt; | ||
|
|
@@ -67,14 +77,17 @@ public class Playing extends BaseCreatedDeletedEntity { | |
| @Column(name = "is_public", nullable = false) | ||
| private boolean isPublic; | ||
|
|
||
| @JdbcTypeCode(SqlTypes.JSON) | ||
| @Column(name = "midi_data", columnDefinition = "jsonb", nullable = false) | ||
| private List<MidiEventData> midiData = new ArrayList<>(); | ||
|
|
||
| @Builder(access = AccessLevel.PRIVATE) | ||
| private Playing( | ||
| Long userId, | ||
| Long backingTrackId, | ||
| PlayingMode mode, | ||
| PlayingStatus status, | ||
| Integer bpm, | ||
| boolean metronomeEnabled, | ||
| boolean isPublic | ||
| ) { | ||
|
|
||
|
|
@@ -86,8 +99,8 @@ private Playing( | |
| this.mode = mode; | ||
|
on1yoneprivate marked this conversation as resolved.
Resolved
|
||
| this.status = status; | ||
| this.bpm = resolveBpm(bpm); | ||
| this.metronomeEnabled = metronomeEnabled; | ||
| this.isPublic = isPublic; | ||
| this.midiData = new ArrayList<>(); | ||
| } | ||
|
|
||
| private static void validateUserId(Long userId) { | ||
|
|
@@ -115,31 +128,54 @@ private static int resolveBpm(Integer bpm) { | |
|
|
||
| // 자유 연주 생성 | ||
| public static Playing createFreePlay( | ||
| Long userId, Integer bpm, boolean metronomeEnabled | ||
| Long userId, Integer bpm | ||
| ) { | ||
| return Playing.builder() | ||
| .userId(userId) | ||
| .mode(PlayingMode.FREE_PLAY) | ||
| .status(PlayingStatus.READY) | ||
| .bpm(bpm) | ||
| .metronomeEnabled(metronomeEnabled) | ||
| .isPublic(false) | ||
| .build(); | ||
| } | ||
|
|
||
| // 백킹트랙 연주 생성 | ||
| public static Playing createBackingTrack( | ||
| Long userId, Long backingTrackId, | ||
| Integer bpm, boolean metronomeEnabled | ||
| Long userId, Long backingTrackId, Integer bpm | ||
| ) { | ||
| return Playing.builder() | ||
| .userId(userId) | ||
| .backingTrackId(backingTrackId) | ||
| .mode(PlayingMode.BACKING_TRACK) | ||
| .status(PlayingStatus.READY) | ||
| .bpm(bpm) | ||
| .metronomeEnabled(metronomeEnabled) | ||
| .isPublic(false) | ||
| .build(); | ||
| } | ||
|
|
||
| public void saveMidiData(List<MidiEventData> midiData) { | ||
|
on1yoneprivate marked this conversation as resolved.
Resolved
|
||
| validateMidiData(midiData); | ||
|
on1yoneprivate marked this conversation as resolved.
Resolved
|
||
|
|
||
| this.midiData = new ArrayList<>(midiData); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. midi 데이터를 저장할 때, 컬렉션 래퍼 참조가 끊기는 것을 방지하기 위해 this.midiData.clear(); 후 addAll() 방식으로 기존 컬렉션 객체를 유지하면서 갱신하는 방식은 어떤가요? 저도 비슷한 걸 찾아보다가 의견 어쭙습니다!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 |
||
| this.midiData.sort(Comparator.comparingLong(MidiEventData::getTimestampMs)); | ||
|
on1yoneprivate marked this conversation as resolved.
Resolved
on1yoneprivate marked this conversation as resolved.
Resolved
|
||
|
|
||
| } | ||
|
|
||
| private static void validateMidiData(List<MidiEventData> midiData) { | ||
|
on1yoneprivate marked this conversation as resolved.
Resolved
|
||
| if (midiData == null || midiData.isEmpty()) { | ||
| throw new GeneralException(PlayingErrorStatus.EMPTY_MIDI_EVENTS); | ||
| } | ||
|
|
||
| if (midiData.stream().anyMatch(Objects::isNull)) { | ||
| throw new GeneralException(PlayingErrorStatus.INVALID_MIDI_EVENT); | ||
| } | ||
| } | ||
|
|
||
| public List<MidiEventData> getMidiData() { | ||
|
|
||
| if (this.midiData == null) { | ||
| return List.of(); | ||
| } | ||
| return List.copyOf(this.midiData); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.