generated from uwu/neptune-template
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shazam - Update to work with latest Neptune, fixes #95
- Loading branch information
Showing
6 changed files
with
76 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import init from "shazamio-core/web"; | ||
import { recognizeBytes, type DecodedSignature } from "shazamio-core/web"; | ||
init(); | ||
|
||
import { v4 } from "uuid"; | ||
|
||
import { requestJsonCached } from "@inrixia/lib/native/request/requestJsonCached.native"; | ||
import type { ShazamData } from "./api.types"; | ||
|
||
export const using = async <T>(signatures: DecodedSignature[], fun: (signatures: ReadonlyArray<DecodedSignature>) => T) => { | ||
const ret = await fun(signatures); | ||
for (const signature of signatures) signature.free(); | ||
return ret; | ||
}; | ||
|
||
export const fetchShazamData = async (signature: { samplems: number; uri: string }) => | ||
requestJsonCached<ShazamData>( | ||
`https://amp.shazam.com/discovery/v5/en-US/US/iphone/-/tag/${v4()}/${v4()}?sync=true&webv3=true&sampling=true&connected=&shazamapiversion=v3&sharehub=true&hubv5minorversion=v5.1&hidelb=true&video=v3`, | ||
{ | ||
headers: { "Content-Type": "application/json" }, | ||
method: "POST", | ||
body: JSON.stringify({ signature }), | ||
} | ||
); | ||
|
||
type Opts = { | ||
bytes: ArrayBuffer; | ||
startInMiddle: boolean; | ||
exitOnFirstMatch: boolean; | ||
}; | ||
|
||
export const recognizeTrack = async ({ bytes, startInMiddle, exitOnFirstMatch }: Opts) => { | ||
const matches: ShazamData[] = []; | ||
await using(recognizeBytes(new Uint8Array(bytes), 0, Number.MAX_SAFE_INTEGER), async (signatures) => { | ||
let i = startInMiddle ? Math.floor(signatures.length / 2) : 1; | ||
for (; i < signatures.length; i += 4) { | ||
const sig = signatures[i]; | ||
const shazamData = await fetchShazamData({ samplems: sig.samplems, uri: sig.uri }); | ||
matches.push(shazamData); | ||
if (shazamData.matches.length === 0) continue; | ||
if (exitOnFirstMatch) return; | ||
} | ||
}); | ||
return matches; | ||
}; |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.