Skip to content

Instantly share code, notes, and snippets.

View lmcarreiro's full-sized avatar

Leonardo Carreiro lmcarreiro

View GitHub Profile
@lmcarreiro
lmcarreiro / zpix-conversion.js
Created May 17, 2023 23:49
Zencastr pixel conversion example
zpix('event', 'purchaseConversion', {
discountCode: '19ninepod', // <-- Replace this value with the show's slug from the URL
});
@lmcarreiro
lmcarreiro / gist:70f49a53502d2ae40e1eeea063546f05
Created May 4, 2023 17:23
Sample of pixel conversions logs
66.249.83.101 - New pixel conversion {"id":"64232c61eaee015ea8f900cb","uid":"1-8jmqul3c-lh8mm2av","eventName":"purchaseConversion","eventData":"{\"purchasePrice\":\"1.00\",\"currency\":\"USD\",\"discountCode\":\"NONE\",\"order\":\"undefined\",\"customerId\":\"0\",\"newCustomer\":true}","version":"1","documentLocation":"https://gtm-msr.appspot.com/render2?id=GTM-HQ4K","referrerLocation":"https://gtm-msr.appspot.com/render?id=GTM-HQ4K","timestamp":"1683174456432","documentEncoding":"UTF-8","screenResolution":"1366x768","viewportResolution":"1366x768","colorDepth":"24","documentTitle":"gtm-msr","browserName":"Chrome 111","isMobile":"false","browserUserAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36","browserTimezone":"300","utm_source":"","utm_medium":"","utm_term":"","utm_content":"","utm_campaign":"","utm_source_platform":"","utm_creative_format":"","utm_marketing_tactic":"","ipAddress":"66.249.83.101","userAgent":"Mozilla/5.0 (Windows N
@lmcarreiro
lmcarreiro / useSpeechToText.ts
Created July 29, 2021 21:28
STT+VAD article - useSpeechToText - Final
import React from "react";
import hark from "hark";
import * as speech from "microsoft-cognitiveservices-speech-sdk";
const AUDIO_SAMPLE_RATE = 48_000;
const BUFFER_SECONDS = 2;
const azureCredentials = {
token: "***********************",
region: "japaneast",
@lmcarreiro
lmcarreiro / useSpeechToText.diff
Created July 29, 2021 21:25
STT+VAD article - useSpeechToText.diff
+ const BUFFER_SECONDS = 2;
export default function useSpeechToText(
speechToTextEnabled: boolean,
muted: boolean,
newMessage: (message: { text: string; isFinal: boolean }) => void,
) {
+ const bufferBlocks = React.useRef<{ duration: number; bytes: ArrayBufferLike }[]>([]);
@lmcarreiro
lmcarreiro / useSpeechToText.diff
Created July 29, 2021 21:21
STT+VAD article - useSpeechToText.diff - 2
// Control the streaming flag, based on the voice activity detection (that uses hark) and the mute/unmute flag
React.useEffect(() => {
if (shouldStream) {
console.log("Voice activity detected, starting streaming current buffer + live streaming...");
streamingFlagRef.current = true;
} else {
console.log("No voice activity detected, stopped streaming.");
+ const timeout = setTimeout(() => {
streamingFlagRef.current = false;
+ }, 2_000);
@lmcarreiro
lmcarreiro / useSpeechToText.diff
Last active July 29, 2021 21:17
STT+VAD article - useSpeechToText.diff
// ...
- const mutedRef = React.useRef<boolean>(muted);
-
- // We use a ref to check if the microphone is muted or not, to avoid recreating
- // the Azure's SpeechRecognizer instance every time we mute/unmute.
- React.useEffect(() => {
- mutedRef.current = muted;
- }, [muted]);
@lmcarreiro
lmcarreiro / useSpeechToText.ts
Last active July 29, 2021 21:12
STT+VAD article - useSpeechToText.ts - 2
// ...
const mutedRef = React.useRef<boolean>(muted);
// We use a ref to check if the microphone is muted or not, to avoid recreating
// the Azure's SpeechRecognizer instance every time we mute/unmute.
React.useEffect(() => {
mutedRef.current = muted;
}, [muted]);
@lmcarreiro
lmcarreiro / useAudioActive.ts
Created July 29, 2021 21:04
STT+VAD article - useAudioActive.ts
import { useEffect, useState } from "react";
import hark from "hark";
export function useAudioActive(stream: MediaStream | undefined) {
const [speakerActive, setSpeakerActive] = React.useState(false);
React.useEffect(() => {
if (!stream) {
setSpeakerActive(false);
return;
@lmcarreiro
lmcarreiro / useSpeechToText.ts
Created July 29, 2021 21:01
STT+VAR article - useSpeechToText.ts
import React from "react";
import hark from "hark";
import * as speech from "microsoft-cognitiveservices-speech-sdk";
const AUDIO_SAMPLE_RATE = 40_000;
const azureCredentials = {
token: "***********************",
region: "japaneast",
};
@lmcarreiro
lmcarreiro / index.tsx
Last active July 29, 2021 20:57
STT+VAR article - index.tsx
import React from "react";
import ReactDOM from "react-dom";
import useSpeechToText from "./utils/hooks/useSpeechToText";
ReactDOM.render(
<React.StrictMode>
<TestApp />
</React.StrictMode>,
document.getElementById("root"),
);