From owner-freebsd-multimedia@freebsd.org Fri Dec 20 22:02:49 2019 Return-Path: Delivered-To: freebsd-multimedia@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 1B85A1E4EF3 for ; Fri, 20 Dec 2019 22:02:49 +0000 (UTC) (envelope-from marcelbonnet@gmail.com) Received: from mail-ot1-x32b.google.com (mail-ot1-x32b.google.com [IPv6:2607:f8b0:4864:20::32b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47fjRD2lMkz4V3M for ; Fri, 20 Dec 2019 22:02:48 +0000 (UTC) (envelope-from marcelbonnet@gmail.com) Received: by mail-ot1-x32b.google.com with SMTP id 77so13600161oty.6 for ; Fri, 20 Dec 2019 14:02:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9fUhNQv1CDBrr9NRVStPMoBNZmek89VqCSt0RO7KV0Q=; b=UJw4AsjmocAEkLIbQ7VZeK5Nufnjybl1KIsfViN9ysYOUjoG1e5YspcDzhzCwcjca8 c0USnR/C4dP0gRMNkFNa5iN71DyOCy0Ggn43T9netNWImJ+XK7hZjMX0QrWyHxp/AaRR 1PckBMSEvYxKTRg8njTeXmSLN3jbHrLQhaYEUtz+c5/1VhAsq4ms1ytLL5t6IjkNXpW9 xOl576/uAKgoqVQokQ1Rrzy8Yko4yDuGy12YevyVJ2jCR1Zihr5XgUEUPrwCeQ7NcS8c e2WhtnCGkOnMkqEQjLrVzu3xTvM/lmhlNq2+9nUZCMjAcvpZWQWeT2pcZKV1cpXGdLoI WJZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9fUhNQv1CDBrr9NRVStPMoBNZmek89VqCSt0RO7KV0Q=; b=SRDU4VjYlr9MZgHC7hhJMs8aYOsevykjYiqoZ3pI23L/QQrDWi/Vl/r8zn8ApBgpeY LeVWkSlytlPpVSTQitjIeSsPIQC0AscYP06zRy8rlBK62uoIc1kZqMR4VZUKaPfHzCtF Xa/rUmaaMRVidAkt3JZkr9EtQf6ZxtYw3OgqqmmWwAOhyzkqrG7JafKign9kTrMJFJ2g uiMF8NIEiZkVictek+kC8tJbTMdJsODB78ysXzqbenGhR0HyZsSLbTXtJk1iyLLYEkcY SbEFOOojjUPBXBkK39C5wnmQMi7qZ4FY/8SlmNxEd4U1tIV60cBkjVBD+WNBeK6Tc6pK p/pw== X-Gm-Message-State: APjAAAXbcFRZVSevePaKlqLMoRi5YcRd+vkVrm9i4W6NgnEVpQ+nU1PI QZ60PotV4MN5NowVcL45JOdqO5NB0Hx5VJO82KcMNVTQ X-Google-Smtp-Source: APXvYqxPpHG6ObW+7zPMzdm+JX4bufbIBg/9bCVRR/ok3W2dCQQUHxgSagNJdg+nhqs1eZJ2fHocqO4K4RA9vLGlzUw= X-Received: by 2002:a05:6830:147:: with SMTP id j7mr17590588otp.44.1576879366564; Fri, 20 Dec 2019 14:02:46 -0800 (PST) MIME-Version: 1.0 References: <77875a51-0cef-a27f-7d27-9eac8426bb74@selasky.org> In-Reply-To: <77875a51-0cef-a27f-7d27-9eac8426bb74@selasky.org> From: Marcel Bonnet Date: Fri, 20 Dec 2019 19:02:33 -0300 Message-ID: Subject: Re: [off-topic] Applying gain to an audio sample To: Hans Petter Selasky Cc: FreeBSD multimedia X-Rspamd-Queue-Id: 47fjRD2lMkz4V3M X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=UJw4Asjm; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of marcelbonnet@gmail.com designates 2607:f8b0:4864:20::32b as permitted sender) smtp.mailfrom=marcelbonnet@gmail.com X-Spamd-Result: default: False [-2.00 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36:c]; FREEMAIL_FROM(0.00)[gmail.com]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-multimedia@freebsd.org]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; IP_SCORE_FREEMAIL(0.00)[]; URI_COUNT_ODD(1.00)[3]; TO_MATCH_ENVRCPT_SOME(0.00)[]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; RCVD_IN_DNSWL_NONE(0.00)[b.2.3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.4.6.8.4.0.b.8.f.7.0.6.2.list.dnswl.org : 127.0.5.0]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; IP_SCORE(0.00)[ip: (-8.02), ipnet: 2607:f8b0::/32(-2.18), asn: 15169(-1.89), country: US(-0.05)]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com.dwl.dnswl.org : 127.0.5.0] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: freebsd-multimedia@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Multimedia discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 Dec 2019 22:02:49 -0000 Em sex, 20 de dez de 2019 15:46, Hans Petter Selasky escreveu: > On 2019-12-20 19:43, Marcel Bonnet wrote: > > - read an audio sample as integer (stereo 16 bit LE) > > - convert to float > > - apply a gain, like: sample *= 0.3f > > - convert to integer again > > The attachment is missing. > Sorry, here it is. #include #include #include #include "/usr/include/sys/soundcard.h" #include #include #include //man 2 chmod #include #define DEBUG 1 #define log(msg) if (DEBUG) printf("[LOG] %s\n",msg) #define err(msg) {printf("[ERR] %s\n",msg); exit(1); } const char *device = "/dev/dsp3.1"; //Audio device char *rawFile = "/tmp/raw-file.wav"; //Raw file to record and playback int fragmentSize = 256; int b_continue = 1; void signalHandler(int sigNum){ log("Signal captured"); b_continue = 0; } void configDevice(int fdDsp){ int ossCapabilities = 0; if(fdDsp == -1) err("can't open device"); if( ioctl(fdDsp, SNDCTL_DSP_GETCAPS, &ossCapabilities) == -1) err("unsupported: SNDCTL_DSP_GETCAPS"); /* * http://www.opensound.com/pguide/audio2.html */ if(ossCapabilities & DSP_CAP_TRIGGER != DSP_CAP_TRIGGER){ err("Triggering of recording/playback is not possible with this OSS device."); } if(ossCapabilities & DSP_CAP_REALTIME != DSP_CAP_REALTIME){ err("No DSP_CAP_REALTIME."); } if(ioctl(fdDsp, SNDCTL_DSP_SETDUPLEX, &ossCapabilities) == -1) err("can't SNDCTL_DSP_SETDUPLEX"); if(ossCapabilities & DSP_CAP_DUPLEX != DSP_CAP_DUPLEX) err("can't DSP_CAP_DUPLEX"); int format = AFMT_S16_LE; //set format if(ioctl(fdDsp, SNDCTL_DSP_SETFMT, &format ) == -1){ err("Error setting format."); } int channels = 1; //mono=0 stereo=1 if(ioctl(fdDsp, SNDCTL_DSP_STEREO, &channels ) == -1){ err("Error setting channels." ); } // FREQUENCY RATE int speed = 44100; if(ioctl(fdDsp, SNDCTL_DSP_SPEED, &speed ) == -1){ err("Error setting speed."); } // FRAGMENT SIZE if(ioctl(fdDsp, SNDCTL_DSP_SETBLKSIZE, &fragmentSize) == -1){ //normalmente 2048 bits err("Cannot SNDCTL_DSP_SETBLKSIZE."); } } void record(){ int fdDsp = open(device, O_RDONLY); configDevice(fdDsp); //create file for writing const int fdOutput = open(rawFile, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR); if(fdOutput ==-1) err("can't open file to write"); log("Recording..."); do{ // Triggers recording int enableBits = PCM_ENABLE_INPUT; if(ioctl(fdDsp, SNDCTL_DSP_SETTRIGGER, &enableBits) == -1) err("Can't record: SNDCTL_DSP_SETTRIGGER"); int *buf[fragmentSize]; read(fdDsp, buf, fragmentSize); write(fdOutput, buf, fragmentSize); } while(b_continue == 1); close(fdOutput); close(fdDsp); } void playback(){ log("Opening file:"); log(rawFile); log("On device:"); log(device); int fdDsp = open(device, O_WRONLY); configDevice(fdDsp); const int fdInput = open(rawFile, O_RDONLY); if(fdInput ==-1) err("can't open file"); log("Playing..."); int eof = 0; do{ // TRIGGERs PLAYBACK int enableBits = PCM_ENABLE_OUTPUT; if(ioctl(fdDsp, SNDCTL_DSP_SETTRIGGER, &enableBits) == -1){ err("Cannot SNDCTL_DSP_SETTRIGGER."); } int buf[fragmentSize]; eof = read(fdInput, buf, fragmentSize); //bytes read or -1 if EOF // audio processing: for(int i=0;i> 16) & 0xffff ; // FIXME: it is causing distortion: float fl = l; float fr = r; fl *= 1.0; fr *= 0.3; //if different than 1, sounds distorted and noisy l = fl; r = fr; // OK: unite Left and Right channels again int lr = (l ) | (r << 16); // OK: other options to mix these two channels: int lleft = l; //Just the left channel int rright = (r << 16); //Just the right channel int lmono = (l << 16) | l; //Left ch. on both channels int rmono = (r << 16) | r; //Right ch. on both channels // the output: (buf)[i] = lr; } write(fdDsp, buf, fragmentSize); if(b_continue == 0) break; } while(eof > 0); close(fdInput); close(fdDsp); } int main(int argc, char *argv[]) { signal(SIGINT, signalHandler); log("Ctrl^C to stop recording/playback"); record(); b_continue = 1; playback(); log("Stopped."); return 0; } > Probably better to use the host endian version of AFMT You mean AFMT_S16_NE ? The native endian? > --HPS >