rrayy commited on
Commit
a7fe853
·
1 Parent(s): 36ffaf1

Changes to be committed: 데이터셋 추가, 토크나이저 수정

Browse files

new file: data/29.mid
new file: data/30.mid
modified: utillity/miditokenizer.py
deleted: utillity/test.py

Files changed (4) hide show
  1. data/29.mid +0 -0
  2. data/30.mid +0 -0
  3. utillity/miditokenizer.py +12 -7
  4. utillity/test.py +0 -70
data/29.mid ADDED
Binary file (1.05 kB). View file
 
data/30.mid ADDED
Binary file (856 Bytes). View file
 
utillity/miditokenizer.py CHANGED
@@ -1,16 +1,21 @@
1
- from miditok import REMI, TokenizerConfig, TokSequence
2
  from symusic import Score
 
3
 
4
  # 토크나이저 설정
5
  config = TokenizerConfig()
6
  tokenizer = REMI(config) # REMI 토크나이저 사용
7
 
8
- def tokenize_midi(midi_num: int):
9
- midi = Score(f"data/{midi_num}.mid")
10
- tokens = tokenizer(midi)
 
11
 
12
- return tokens
 
 
13
 
14
- def detokenize_midi(tokens:TokSequence):
15
- midi = tokenizer(tokens)
 
16
  return midi
 
1
+ from miditok import REMI, TokSequence, TokenizerConfig
2
  from symusic import Score
3
+ import os
4
 
5
  # 토크나이저 설정
6
  config = TokenizerConfig()
7
  tokenizer = REMI(config) # REMI 토크나이저 사용
8
 
9
+ def get_midi_scores(midi_folder):
10
+ for filename in os.listdir(midi_folder):
11
+ if filename.endswith(".mid"):
12
+ yield Score(os.path.join(midi_folder, filename)) # 제네레이터로 코루틴 만들어서 이용
13
 
14
+ def tokenize_midi(midi):
15
+ tokens = tokenizer(midi)
16
+ return tokens[0].ids # type: ignore
17
 
18
+ def detokenize_midi(tokens: list[int]):
19
+ seq = TokSequence(ids=tokens) # type: ignore
20
+ midi = tokenizer.decode([seq])
21
  return midi
utillity/test.py DELETED
@@ -1,70 +0,0 @@
1
- import pretty_midi
2
- import random
3
-
4
- # 슬픈 분위기: A minor 스케일
5
- A_MINOR_SCALE = [69, 71, 72, 74, 76, 77, 79] # A B C D E F G
6
-
7
- # 코드 진행 (A minor 중심)
8
- CHORD_PROGRESSION = ['Am', 'F', 'C', 'G']
9
- CHORD_DICT = {
10
- 'Am': [69, 72, 76], # A C E
11
- 'F': [65, 69, 72], # F A C
12
- 'C': [60, 64, 67], # C E G
13
- 'G': [67, 71, 74], # G B D
14
- }
15
-
16
- # 설정값
17
- BPM = 90
18
- CHORD_DURATION = 1.0
19
- NOTE_RESOLUTION = 0.5
20
- VELOCITY_CHORD = 70
21
- VELOCITY_MELODY = 90
22
-
23
- # MIDI 객체 생성
24
- midi = pretty_midi.PrettyMIDI()
25
- chord_track = pretty_midi.Instrument(program=0) # 피아노
26
- melody_track = pretty_midi.Instrument(program=40) # 바이올린
27
-
28
- # 초기 시간
29
- current_time = 0.0
30
-
31
- # 코드 진행 순회
32
- for chord_name in CHORD_PROGRESSION:
33
- chord_pitches = CHORD_DICT[chord_name]
34
-
35
- # 코드 입력
36
- for pitch in chord_pitches:
37
- chord_track.notes.append(pretty_midi.Note(
38
- velocity=VELOCITY_CHORD,
39
- pitch=pitch,
40
- start=current_time,
41
- end=current_time + CHORD_DURATION
42
- ))
43
-
44
- # 멜로디 구성 (슬픈 느낌: 자연단음계에서 부드럽게 흐름)
45
- time_cursor = current_time
46
- previous_pitch = None
47
-
48
- while time_cursor < current_time + CHORD_DURATION:
49
- pitch = random.choice(A_MINOR_SCALE)
50
-
51
- # 음 높이 흐름 부드럽게
52
- if previous_pitch and abs(pitch - previous_pitch) > 5:
53
- pitch = previous_pitch + random.choice([-2, -1, 1, 2])
54
-
55
- melody_track.notes.append(pretty_midi.Note(
56
- velocity=VELOCITY_MELODY,
57
- pitch=pitch,
58
- start=time_cursor,
59
- end=time_cursor + NOTE_RESOLUTION
60
- ))
61
- previous_pitch = pitch
62
- time_cursor += NOTE_RESOLUTION
63
-
64
- # 다음 코드로 이동
65
- current_time += CHORD_DURATION
66
-
67
- # 트랙 추가 및 MIDI 파일 저장
68
- midi.instruments.append(chord_track)
69
- midi.instruments.append(melody_track)
70
- midi.write('sad_ambient_melody.mid')