Skip to content

Commit

Permalink
wip: initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelduarte077 committed Aug 26, 2024
0 parents commit e4f86aa
Show file tree
Hide file tree
Showing 20 changed files with 798 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
410 changes: 410 additions & 0 deletions ShazamLyrics.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>ShazamLyrics.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>ShazamLyrics.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
11 changes: 11 additions & 0 deletions ShazamLyrics/Assets.xcassets/AccentColor.colorset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
98 changes: 98 additions & 0 deletions ShazamLyrics/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions ShazamLyrics/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
9 changes: 9 additions & 0 deletions ShazamLyrics/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import SwiftUI

struct ContentView: View {
var body: some View {
ShazamView()
}
}


6 changes: 6 additions & 0 deletions ShazamLyrics/Models/LyricsModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

struct LyricsModel: Codable {
let lyrics : String?
let error : String?
}
8 changes: 8 additions & 0 deletions ShazamLyrics/Models/ShazamModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

struct ShazamModel : Decodable {

let title: String?
let artist : String?
let album : URL?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
10 changes: 10 additions & 0 deletions ShazamLyrics/ShazamLyricsApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SwiftUI

@main
struct ShazamLyricsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
24 changes: 24 additions & 0 deletions ShazamLyrics/ViewModels/LyricsViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

@MainActor

class LyricsViewModel : ObservableObject {

@Published var lyricModel = LyricsModel(lyrics: "", error: "")

func fetch(artist: String, title: String) async {
do{
let urlStr = "https://api.lyrics.ovh/v1/\(artist)/\(title)"
guard let url = URL(string: urlStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) ?? "") else { return }


let (data, _) = try await URLSession.shared.data(from: url)
let datos = try JSONDecoder().decode(LyricsModel.self, from: data)

self.lyricModel = datos
}catch let error as NSError{
print("error", error.localizedDescription)
}
}

}
81 changes: 81 additions & 0 deletions ShazamLyrics/ViewModels/ShazamViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import ShazamKit
import AVKit
import SwiftUI

class ShazamViewModel: NSObject, ObservableObject {

@Published var shazamModel = ShazamModel(title: "Pulsa", artist: "Escuchar", album: URL(string: "https://google.com"))
@Published var recording = false

private var audioEngine = AVAudioEngine()
private var session = SHSession()
private var signatureGenerator = SHSignatureGenerator()

override init(){
super.init()
session.delegate = self
}

func startListening(){
guard !audioEngine.isRunning else {
audioEngine.stop()
DispatchQueue.main.async {
self.recording = true
}
return
}

let audioSession = AVAudioSession.sharedInstance()
audioSession.requestRecordPermission { granted in
guard granted else { return }

try? audioSession.setActive(true, options: .notifyOthersOnDeactivation)
let inputNode = self.audioEngine.inputNode
let recordingFormat = inputNode.outputFormat(forBus: 0)

inputNode.removeTap(onBus: .zero)

inputNode.installTap(onBus: .zero, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
self.session.matchStreamingBuffer(buffer, at: nil)
}

self.audioEngine.prepare()
do{
try self.audioEngine.start()
}catch let error as NSError{
print("Error al escanear", error.localizedDescription)
}

DispatchQueue.main.async {
self.recording = true
}

}
}

func stop(){
if audioEngine.isRunning {
audioEngine.stop()
DispatchQueue.main.async {
self.recording = false
}
}
}

}

extension ShazamViewModel: SHSessionDelegate {

func session(_ session: SHSession, didFind match: SHMatch) {
let mediaItems = match.mediaItems
if let item = mediaItems.first {
DispatchQueue.main.async {
self.shazamModel = ShazamModel(title: item.title, artist: item.artist, album: item.artworkURL)
if ((self.shazamModel.album?.isFileURL) != nil) {
self.stop()
}
}
}
}

}
29 changes: 29 additions & 0 deletions ShazamLyrics/Views/LyricsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import SwiftUI

struct LyricsView: View {

@StateObject private var lyrics = LyricsViewModel()
var artist: String
var title: String

var body: some View {
VStack{
Text(title)
.font(.title2)
.foregroundColor(.orange)
.bold()

ScrollView{
Text(lyrics.lyricModel.lyrics ?? "Letra no encontrada")
.bold()
.multilineTextAlignment(.center)
.foregroundColor(.orange)
}
}.navigationTitle(artist)
.task {
await lyrics.fetch(artist: artist, title: title)
}
}
}


Loading

0 comments on commit e4f86aa

Please sign in to comment.