Skip to content

Instantly share code, notes, and snippets.

@mjago
mjago / play.cr
Created July 9, 2018 05:28
logger
require "logger"
log = Logger.new(STDOUT)
log.level = Logger::WARN
critical = Logger.new(STDERR)
log.debug("Created logger")
log.info("Program started")
log.warn("Nothing to do!")
critical.error("Critical error!")
@mjago
mjago / type_spec.cr
Created July 2, 2018 13:42
Avoiding requiring spec_helper means test runs in 3 sec versus over a minute
#require "../../spec_helper"
require "spec"
describe "Semantic: pointer" do
it "types int pointer" do
# assert_type("a = 1; pointerof(a)") { pointer_of(int32) }
(a = 1; pointerof(a)).class.should eq(Pointer(Int32))
end
require "db"
require "sqlite3"
require "benchmark"
QUERY = "select id from numbers"
DB.open "sqlite3:./file.db" do |db|
db.exec %q{DROP TABLE `numbers`}
end
DB.open "sqlite3:./file.db" do |db|
@mjago
mjago / play.cr
Last active June 25, 2018 17:54
Simple Command-line audio player with VU-meter in Crystal (0.25.0)
require "libao"
include Libao
# Simple Command Line Audio Player with VU-meter in Crystal (0.25.0)
# Requires ffmpeg and libao-dev
# dependencies:
# shard dependency: github.com/mjago/libao
# Tested on mp3, flac, and m4a files
class Converter
PIPE = Process::Redirect::Pipe
Firs look at node visualisation
This is top-down but can be changed to left-to-right with some fiddling
@mjago
mjago / load_wav.cr
Last active March 30, 2017 05:58
example of Mix_LoadWAV_RW
require "../src/sdl"
require "../src/mixer"
SDL.init(SDL::Init::VIDEO | SDL::Init::AUDIO); at_exit { SDL.quit }
SDL::Mixer.init(SDL::Mixer::Init::MP3); at_exit { SDL::Mixer.quit }
SDL::Mixer.open
chunk = {} of String => LibMixer::Mix_Chunk*
names = %w(high medium low scratch)
lib SomeLib
fun some_fun(LibC::Char*)
end
class SomeClass
def some_fun(arg : String)
SomeLib.some_fun(arg)
end
end
diff -u /Users/martyn/.cache/crystal/initial_build/ /Users/martyn/.cache/crystal/Users-martyn-crystal-compile-empty.cr/
Binary files /Users/martyn/.cache/crystal/initial_build/H-ash40T-hread4432N-il41.bc and /Users/martyn/.cache/crystal/Users-martyn-crystal-compile-empty.cr/H-ash40T-hread4432N-il41.bc differ
Binary files /Users/martyn/.cache/crystal/initial_build/S-et40T-hread41.bc and /Users/martyn/.cache/crystal/Users-martyn-crystal-compile-empty.cr/S-et40T-hread41.bc differ
Binary files /Users/martyn/.cache/crystal/initial_build/T-hread.bc and /Users/martyn/.cache/crystal/Users-martyn-crystal-compile-empty.cr/T-hread.bc differ
* Initial Build:
** H-ash40T-hread4432N-il41.bc
debug info...
Hash(Thread, Nil).CrystalDebug Info Versionhash.cr/usr/local/Cellar/crystal-lang/HEAD-102be34_2/srcint
initialize:initial_capacity
@mjago
mjago / gist:65dfcf87685b891696a309b691426ff5
Created January 31, 2017 10:18
DWARF warnings running specs
llvm-config 3.9.1
Crystal 0.20.5+27 [03e69b7] (2017-01-31)
clang version 3.9.1 (tags/RELEASE_391/final)
Tue Jan 31: > ./bin/crystal spec spec/compiler_spec.cr
Using compiled compiler at .build/crystal
ld: warning: unknown dwarf DW_FORM_strp (offset=0x00000080) is too big in E-xception.o
ld: warning: unknown dwarf DW_FORM_strp (offset=0x00000080) is too big in H-ash40T-hread4432N-il41.o
@mjago
mjago / gist:78afea9f5aefde8b517bfdcd82937076
Created January 25, 2017 11:28
hello_world with release and non-release compilers
non release compiler:
Wed Jan 25: compile/ > bin/crystal ../examples/hello/hello.cr --
release --stats
Using compiled compiler at .build/crystal
Parse: 00:00:00.0409620 ( 0.19MB)
Semantic (top level): 00:00:00.5428400 ( 26.57MB)
Semantic (new): 00:00:00.0078450 ( 26.57MB)
Semantic (type declarations): 00:00:00.0535600 ( 26.57MB)
Semantic (abstract def check): 00:00:00.0026690 ( 26.57MB)
Semantic (cvars initializers): 00:00:00.0243660 ( 34.57MB)