Skip to content

Instantly share code, notes, and snippets.

@polson
polson / train_dataloader.py
Last active July 5, 2025 19:26
A dataloader that creates random mixtures from stems. Useful for datasets like MUSDB18HQ
import random
import random
import sys
import time
import threading
import queue
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Optional, Tuple
@polson
polson / scnet.py
Created June 28, 2025 15:51
SCNet Masked V2
import math
from collections import deque
import torch
import torch.nn as nn
import torch.nn.functional as F
from .separation import SeparationNet
class BetterDialog<T> : DialogFragment() {
companion object {
val KEY_ARGS = "KEY_ARGS"
val KEY_CALLED_FROM_FRAGMENT = "KEY_CALLED_FROM_FRAGMENT"
val TAG = "DIALOG_TAG"
}
@Suppress("UNCHECKED_CAST")
private val args: DialogBuilderImpl.DialogArgs<T> by lazy {
checkNotNull(arguments?.getSerializable(KEY_ARGS) as DialogBuilderImpl.DialogArgs<T>) { "Args are null!" }
private class DialogBuilderImpl<T>(val context: Context) : DialogBuilder<T> {
data class DialogArgs<T>(
val title: String? = null,
val message: String? = null,
val yesButtonAction: Serializable? = null,
val noButtonAction: Serializable? = null,
val yesButtonText: String? = null,
val noButtonText: String? = null
) : Serializable
private typealias DialogCallback<T> = T.() -> Unit
interface DialogBuilder<T> {
fun title(title: String)
fun title(titleResId: Int)
fun message(message: String)
fun message(messageResId: Int)
class BetterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener { showBetterDialog() }
val normalButton = findViewById<Button>(R.id.normalButton)
normalButton.setOnClickListener { launchNormalActivity() }
class NormalActivity : AppCompatActivity(), NormalDialogFragment.NormalDialogListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_normal)
val button = findViewById<Button>(R.id.normalButton)
button.setOnClickListener { showNormalDialog() }
}
private fun showNormalDialog() =
/**
* Provides a single instance of a class requiring a context. Can be inherited by a companion
* object to easily make it a singleton
*/
abstract class SingletonWithContext<T> {
@Volatile
private var instance: T? = null
fun getInstance(context: Context): T =
instance ?: synchronized(this) {
@polson
polson / recycler_view_with_header.xml
Last active July 11, 2019 21:04
Example of how to add a header to a RecyclerView in XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
@polson
polson / ActivityFromContext.kt
Created May 22, 2019 17:18
Method to get an activity instance from a context
private fun scanForActivity(context: Context?): FragmentActivity? = when (context) {
is FragmentActivity -> context
is ContextWrapper -> scanForActivity(context.baseContext)
else -> throw IllegalArgumentException("Context must be a FragmentActivity!")
}