Re: Problems using rawWrite in an experiment with WAVs in D
On Wednesday, 27 December 2023 at 20:20:23 UTC, tososdk wrote: I was recreating some code from C++ to D: [...] But since I am somewhat new to these topics and even more so to Dlang, I don't understand very well. The problem occurs in the creation of the .wav, regarding rawWrite, I'm not really sure what to do in that specific part. Maybe I'm ignorant, but I'm not very good at these topics, plus it's for experimentation. Here's the error message I got when I tried to compile your code: ``` Error: template `std.stdio.File.rawWrite` is not callable using argument types `!()(WavHeader)` /dlang/dmd/linux/bin64/../../src/phobos/std/stdio.d(1273): Candidate is: `rawWrite(T)(in T[] buffer)` ``` What this message is saying is that you tried to pass a `WavHeader` to `rawWrite`, but `rawWrite` expects a slice (`T[]`, where `T` can be any type) as an argument. The easiest way to fix this is to use [pointer slicing][1] to create a temporary slice that points to `wavh`: ```d file.rawWrite((&wahv)[0 .. 1]); ``` [1]: https://dlang.org/spec/expression.html#slice_expressions
Re: Problems using rawWrite in an experiment with WAVs in D
Because WaveHeader has no pointers in it, only raw memory, assuming it is all in the cpu endianesss and with ``align(1)`` you can slice that block of stack memory and write from that. ```d file.rawWrite((cast(ubyte*)&wahv)[0 .. WaveHeader.sizeof]); ``` However I would recommend doing it field by field. A lot more work, but allows you to handle endianness issues and remove alignment concerns. Also changing of field sizes if required. Same principles as the code above.
Problems using rawWrite in an experiment with WAVs in D
I was recreating some code from C++ to D: ``` import std.stdio; import std.file; import std.string; import std.math; struct WavHeader { char[4] riff; int flength; char[4] wave; char[4] fmt; int chunk_size; short format_tag; short num_chans; int sample_rate; int bytes_per_second; short bytes_per_sample; short bits_per_sample; char[4] data; int dlength; } void main() { WavHeader wahv; wahv.riff[] = "RIFF".dup; wahv.wave[] = "WAVE".dup; wahv.fmt[] = "fmt ".dup; wahv.data[] = "data".dup; wahv.chunk_size = 16; wahv.format_tag = 1; wahv.num_chans = 1; wahv.sample_rate = 8000; wahv.bits_per_sample = 16; wahv.bytes_per_sample = cast(short)((wahv.bits_per_sample / 8) * wahv.num_chans); wahv.bytes_per_second = wahv.sample_rate * wahv.bytes_per_sample; const int duration_seconds = 10; const int buffer_size = wahv.sample_rate * duration_seconds; wahv.dlength = buffer_size * wahv.bytes_per_sample; wahv.flength = wahv.dlength + 44; short[] buffer = new short[buffer_size]; foreach (i; 0 .. buffer_size) { buffer[i] = cast(short)(cos((2.0 * PI * 256.0 * i) / wahv.sample_rate) * 1000); } // Corrected file handling auto file = File("test.wav", "r"); file.rawWrite(wahv); // Writing the audio data as raw bytes file.rawWrite(cast(ubyte[])buffer); file.close(); } ``` But since I am somewhat new to these topics and even more so to Dlang, I don't understand very well. The problem occurs in the creation of the .wav, regarding rawWrite, I'm not really sure what to do in that specific part. Maybe I'm ignorant, but I'm not very good at these topics, plus it's for experimentation.
Re: Something similar to "inline"
On Wednesday, 27 December 2023 at 16:35:47 UTC, Paul Backus wrote: On Wednesday, 27 December 2023 at 15:57:14 UTC, tososdk wrote: Two things: Could you explain how "inline" works? Is there something similar in Dlang? I don't think the D forums is the best place to ask about how "inline" works, when you mean another "inline" than what is available in D... In C and C++, `inline` is a suggestion to the compiler that it should consider using [inline expansion][1] for calls to a particular function. However, the compiler is free to ignore this suggestion, and is also free to use inline expansion for functions that are not marked with `inline`. `inline` in C/C++ is only vaguely related to inline expansion. Its meaning is related to linkage, allowing multiple definitions of that symbol. https://en.cppreference.com/w/cpp/language/inline Dlang does not have something similar. -Johan
Re: Something similar to "inline"
On Wednesday, 27 December 2023 at 15:57:14 UTC, tososdk wrote: Two things: Could you explain how "inline" works? Is there something similar in Dlang? In C and C++, `inline` is a suggestion to the compiler that it should consider using [inline expansion][1] for calls to a particular function. However, the compiler is free to ignore this suggestion, and is also free to use inline expansion for functions that are not marked with `inline`. In D, the closest equivalent is [`pragma(inline)`][2], which can be used to enable or disable inline expansion for a particular function. With `pragma(inline, true)`, the function will *always* be expanded inline; with `pragma(inline, false)`, it will *never* be expanded. [1]: https://en.wikipedia.org/wiki/Inline_expansion [2]: https://dlang.org/spec/pragma.html#inline
Re: Non-blocking keyboard input
On Wednesday, 27 December 2023 at 14:41:05 UTC, Christian Köstlin wrote: One option (not tested) should be to close stdin so that readln then returns null or something on eof. Shutting down threads is always tricky. It would be great if there would be one or two (perhaps one synchronous, one asynchronous) main io-frameworks for dlang (also as dub packages) that cover the most common use-cases. Kind regards, Christian I tested this now, but it still blocks in readln ... Kind regards, Christian
Re: Non-blocking keyboard input
On Wednesday, 27 December 2023 at 05:07:04 UTC, Joe wrote: ??? Surely there there is a one liner library solution for this? I have a program that spawns a thread for debugging information and uses the keyboard input which allows me to display the information. If I use getchar or readline then it blocks the thread. This is generally fine because that is all the thread does. The problem is that it also blocks the program termination as the main thread will not exit while the thread is running which is is because it's waiting on keyboard input stuck on getchar or fgetc or whatever. If I terminate the threads using thread_term then it terminates the program but the program then does not return the return code that it was successfully finished(because it was prematurely terminated by thread_term. Surely there is some type of peek for keyboards in D? I can't seem to get kbhit or because the library is not included(well, I tried to use one from dmc but it seemed to be invalid. snn.lib IIRC). This shouldn't be hard... yet it is. One option (not tested) should be to close stdin so that readln then returns null or something on eof. Shutting down threads is always tricky. It would be great if there would be one or two (perhaps one synchronous, one asynchronous) main io-frameworks for dlang (also as dub packages) that cover the most common use-cases. Kind regards, Christian
Re: Behaves different on my osx and linux machines
On Wednesday, 27 December 2023 at 14:03:06 UTC, Kagamin wrote: Maybe you're not supposed to print text while reading? In parallel I have contacted schveiguy on discord and he found the culprid. But we do not have a solution yet. It probably will result in a bugreport at https://issues.dlang.org/. Kind regards, Christian
Re: Behaves different on my osx and linux machines
Maybe you're not supposed to print text while reading?
Re: Behaves different on my osx and linux machines
Maybe write and read lock each other, try to use puts: ``` bool done = false; while (!done) { puts("1"); auto result = ["echo", "Hello World"].execute; if (result.status != 0) { writeln(2); throw new Exception("echo failed"); } puts("2"); writeln(result.output); puts("3"); receiveTimeout(dur!"msecs"(-1), (LinkTerminated t) { writeln("Done"); done = true; }, ); writeln(4); } writeln("ByeBye"); ```
Re: Non-blocking keyboard input
On Wednesday, 27 December 2023 at 05:07:04 UTC, Joe wrote: ??? Surely there there is a one liner library solution for this? It is not one line because it needs a bit of setup (and teardown, but the objects' destructors do that for you) but it is close: http://arsd-official.dpldocs.info/arsd.terminal.html#single-key `input.getch` waits for a single line, but you can use `if(input.kbhit())` to see if it would block before calling it. This shouldn't be hard... yet it is. Better be careful, the mods are out in force deleting posts this week that tell the hard truth. But yeah, the stdlib in D has very little activity: https://github.com/dlang/phobos/graphs/contributors? So you can't expect much from it. My arsd libs provide a broad set of functionality missing from it: stuff like this terminal/console stuff, window creation, basic guis, web servers, etc. If you want to try them, you can use it from the dub system, but I recommend just `git clone https://github.com/adamdruppe/arsd.git` in your working directory then import what you want and use `dmd -i` to automatically include them in the build.
Looking for some thoughtful review of my first D program
Hi, you all ! I've wanted to look at D for the last two years, being a C fanboy and not fond of C++. It seemed like a good alternative between some delightful extra new stuff and keeping C-like experience : performances and code style wise. I wrote my first CLI program this weekend and would like some code review, if some of you don't mind. :) The source code can be found here : https://git.sr.ht/~nasmevka/tmp/tree/main/item/tmp.d I apologize for the way my code looks like. I do not use LSPs and having function definition's name on their own line make `grep ^funcname` really powerful. Templates are really powerful, and I feel like I do not use them near enough ? 🤷 Open to all form of criticism ☺️