-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
160 lines (145 loc) · 6.61 KB
/
ContentView.swift
File metadata and controls
160 lines (145 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import SwiftUI
struct ContentView: View {
@State private var image: UIImage?
@State private var showSongOptions = false
@State private var isProcessing = false
@State private var selectedAudioURL: URL?
@State private var selectedSongTitle: String?
let spaceSongURL = Bundle.main.url(forResource: "spacesong", withExtension: "mp3")
let starshipsURL = Bundle.main.url(forResource: "kerosene", withExtension: "mp3")
var body: some View {
VStack {
Text("MUSE")
.font(.largeTitle)
.fontWeight(.bold)
.dynamicTypeSize(/*@START_MENU_TOKEN@*/.xLarge/*@END_MENU_TOKEN@*/)
.padding(.bottom, 50)
ZStack {
Rectangle()
.fill(Color.white.opacity(0.4))
.frame(width: 800, height: 600)
.cornerRadius(20)
VStack {
if let image = image {
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(height: 400)
.padding()
} else {
Spacer()
}
if showSongOptions {
HStack(spacing: 40) {
Button(action: {
selectedAudioURL = spaceSongURL
selectedSongTitle = "Space Song"
showSongOptions = false
print("space song selected")
}) {
Text("space song")
.frame(width: 160)
}
.padding()
.background(Color.black.opacity(0.8))
.cornerRadius(8)
.frame(width: 160)
Button(action: {
selectedAudioURL = starshipsURL
selectedSongTitle = "kerosene"
showSongOptions = false
print("kerosene selected")
}) {
Text("kerosene")
.frame(width: 160)
}
.padding()
.background(Color.black.opacity(0.8))
.cornerRadius(8)
.frame(width: 160)
}
.padding()
} else {
if selectedAudioURL != nil && !isProcessing {
Button(action: processSong) {
Text("create")
}
.padding()
.background(Color.black.opacity(0.8))
.cornerRadius(8)
Button(action: {
showSongOptions = true
}) {
Text("songs")
}
.padding()
.background(Color.black.opacity(0.8))
.cornerRadius(8)
Spacer()
Spacer()
} else if isProcessing {
Text("generating....")
.padding()
.background(Color.red.opacity(0.8))
.cornerRadius(8)
} else {
Button(action: {
showSongOptions = true
image = nil
}) {
Text("songs")
}
.padding()
.background(Color.black.opacity(0.8))
.cornerRadius(8)
Spacer()
Spacer()
}
}
}
.frame(width: 300, height: 200) // Ensure the VStack frame matches the rectangle
}
Spacer()
}
Spacer() // Add space to center the ZStack vertically when there is no image
}
func processSong() {
guard let audioURL = selectedAudioURL else { return }
isProcessing = true
print("Processing song: \(audioURL.absoluteString)")
let analyzer = AudioAnalyzer()
if let features = analyzer.extractFeatures(from: audioURL) {
let description = generateDescription(from: features, songTitle: selectedSongTitle ?? "the song")
print("Description generated: \(description)")
DalleAPI.generateImage(from: description) { imageURL in
guard let imageURL = imageURL else {
print("Failed to get image URL")
self.isProcessing = false
return
}
print("Image URL: \(imageURL)")
if let imageData = try? Data(contentsOf: imageURL),
let image = UIImage(data: imageData) {
DispatchQueue.main.async {
print("Image fetched successfully")
self.image = image
self.isProcessing = false
}
} else {
DispatchQueue.main.async {
print("Failed to load image data")
self.isProcessing = false
}
}
}
} else {
print("Failed to extract audio features")
isProcessing = false
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}